Paging Through the Result Set in Hibernate
By: Sumit Pal
Many a times you would need to retrieve a large number of records but on the webpage you would show them in multiple pages. For example, in a tracking application, you want to show a history tracking page, the query would return hundreds or even thousands of records. But the webpage you may want to show fifty records on a page and do a simple previous and next links or show page numbers as links to show fifty records in each page.
Pagination through the result set of a database query is a very common application pattern. Typically, you would use pagination for a web application that returned a large set of data for a query. The web application would page through the database query result set to build the appropriate page for the user. The application would be very slow if the web application loaded all of the data into memory for each user. Instead, you can page through the result set and retrieve the results you are going to display one chunk at a time.
There are two methods on the Query interface for paging: setFirstResult() and setMaxResults(), just as with the Criteria interface. The setFirstResult() method takes an integer that represents the first row in your result set, starting with row 0. You can tell Hibernate to only retrieve a fixed number of objects with the setMaxResults() method. Your HQL is unchanged—you only need to modify the Java code that executes the query.
Query query = session.createQuery("from Product");
query.setFirstResult(1);
query.setMaxResults(2);
List results = query.list();
displayProductsList(results);
You can change the numbers around and play with the pagination. If you turn on SQL logging, you can see which SQL commands Hibernate uses for pagination. For the open source HSQLDB database, Hibernate uses top and limit.
Archived Comments
1. nfyuphCep
View Tutorial By: nvcmeqCep at 2017-09-13 06:01:39
2. Brettkip
View Tutorial By: Brettkip at 2017-06-06 03:31:50
3. After coming to second page again I want to go to first page then what should I do?
View Tutorial By: Bhagi at 2012-09-23 06:25:54
4. i have to retrieve data from database which is on some constraint and will display in a webpage.
View Tutorial By: abhishek at 2012-04-05 05:04:01
5. can u give me the structure of displayProductList(results) function.
View Tutorial By: swapnil at 2011-09-22 04:41:24
6. Hi Along with your provided code below, if we are looking for total number of rows (only count) then
View Tutorial By: Roshan at 2011-05-02 12:06:27
7. I have a HSQL query that uses many tables and I'm not sure how to access the results.
View Tutorial By: Kris Reid at 2009-11-04 15:23:40
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Primary keys assigned by triggers in Hibernate
Assigned identifiers in Hibernate
Identity columns and sequences in Hibernate
EntityNameResolvers in Hibernate
Tuplizers (org.hibernate.tuple.Tuplizer) in Hibernate
equals() and hashCode() in Hibernate
Fetching strategies in Hibernate
Creating Connection Pool for JDBC Connections in Hibernate
Hibernate JDBC and Connection Properties
Hibernate Transaction Properties