javadatabasehibernatepagination

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


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?


Solution

  • 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.