DisplayTag pagination vs Hibernate Pagination

随声附和 提交于 2019-11-29 07:57:41

As you outlined, with the DisplayTag you need to fetch all the results and to put them in the session. So you're doing only one (potentially expensive) query but this won't scale well from a memory point of view (if you need to fetch a high number of results or if you increase the number of concurrent users).

On the other hand, with Hibernate, you can use setFirstResult and setMaxResult to fetch only the records actually shown on each page. This requires to perform a query for each page but will scale for an infinite number of result.

Personally, I prefer the second approach that I find more memory efficient - especially since most users don't browse all pages (so why loading all results) - and use the pattern described in Pagination in Hibernate and EJB3.

If you decide to stick with the first approach, I'd implement some kind of max results number limit to avoid too expensive queries. If a query goes beyond the limit, ask the user to perform a more restrictive search i.e. to add criteria (who is going to browser several thousands of results anyway?).

And if you need all the results, for example for reporting purposes, then neither the DisplayTag nor the statefull session is the right tool in my opinion.

Daniel Serodio

You don't actually need to fetch all results, you can use a Displaytag feature called external pagination, where you tell Displaytag that you're handling the pagination outside Displaytag.

See External Paging and Sorting in Displaytag's documentation for details.

Also, this stackoverflow question has more info and sample code.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!