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.