Display Tag provides pagination feature from the given object. Hibernates provides option to fetch only required record per page.In My project we are using both the concept.
Display Tag: I need to fetch all the record based on the filter criteria and store it in the Session. Then this displaytag will take care about all pagination and sorting.So Httpsession holds lot of data.
Hibernate: It fetches only the requested no of Objects from the database.Need to open session for every request.
What is the Best way of doing?Or if we can acheive both the things how to do that? Please help on this.
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.
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
andsetMaxResult
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.