permaculture playing cards*
The moose likes Object Relational Mapping and the fly likes Finding the total result size using Hibernate Query and pagination Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Finding the total result size using Hibernate Query and pagination" Watch "Finding the total result size using Hibernate Query and pagination" New topic
Author

Finding the total result size using Hibernate Query and pagination

David Irwin
Ranch Hand

Joined: Mar 25, 2004
Posts: 82
All,

I'm using Hibernate/HQL/named queries to retrieve data from a database. In particular I use pagination (i.e. I control the number of results that are returned on any one query) so as not to pull thousands of rows from the database.

Here's the java code I have right now:



What I'd like to be able to do, however, is find out how many total results this query will return independent of the pagination (so I can display something like "Results 31 through 40 of 123" to the end user). I've looked at the Hibernate Query API but don't see anything help. Any ideas?

Thanks,
Dave
Angel Taveras
Ranch Hand

Joined: Nov 13, 2008
Posts: 84

Hello David,

I been there before and what i did was kinda like a double query, one for the result and the other one for the count of the unpaged results. Hibernate do have a ScrollableResult that could help you a little with this.

This it's a reality that many of us encounter time to time when working with db, because of the paging and all that stuff, but there it's a pattern to solve this. Here explains about the Pagination pattern with diagrams and stuff.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Finding the total result size using Hibernate Query and pagination