Implementing result paging in hibernate (getting total number of rows)

flybywire picture flybywire · Oct 21, 2009 · Viewed 19.2k times · Source

How do I implement paging in Hibernate? The Query objects has methods called setMaxResults and setFirstResult which are certainly helpful. But where can I get the total number of results, so that I can show link to last page of results, and print things such as results 200 to 250 of xxx?

Answer

sinuhepop picture sinuhepop · Oct 21, 2009

You can use Query.setMaxResults(int results) and Query.setFirstResult(int offset).

Editing too: There's no way to know how many results you'll get. So, first you must query with "select count(*)...". A little ugly, IMHO.