Paging Through the Result Set in Hibernate

By: Sumit Pal Emailed: 1676 times Printed: 2158 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

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.


Hibernate Home | All Hibernate Tutorials | Latest Hibernate Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(4)


1. View Comment

I have a HSQL query that uses many tables and I'm not sure how to access the results.

Any chance you can explain?

I posted a question https://forum.hibernate.org/viewtopic.php?f=1&t=1000766
But a tutorial would be great


View Tutorial          By: Kris Reid at 2009-11-04 15:23:40
2. View Comment

Hi Along with your provided code below, if we are looking for total number of rows (only count) then what we need to do?:

Query query = session.createQuery("from Product");
query.setFirstResult(1);
query.setMaxResults(2);
List results = query.list();
displayProductsList(results);

Thanks in advance,
Regards,
Roshan


View Tutorial          By: Roshan at 2011-05-02 12:06:27
3. View Comment

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
4. View Comment

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

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Sumit Pal
Paging Through the Result Set in Hibernate

More Tutorials in Hibernate
Fetching strategies in Hibernate
equals() and hashCode() in Hibernate
Tuplizers (org.hibernate.tuple.Tuplizer) in Hibernate
EntityNameResolvers in Hibernate
Hi/lo algorithm in Hibernate
Identity columns and sequences in Hibernate
Assigned identifiers in Hibernate
Primary keys assigned by triggers in Hibernate
Formula in Hibernate
Step by Step Hibernate - Your First Hibernate Application
A sample Hibernate Web Application using Servlets
Some Basic APIs in Hibernate
Contextual sessions in Hibernate
Programmatic configuration in Hibernate
SessionFactory in Hibernate

More Latest News
Most Viewed Articles (in Hibernate )
Hibernate Vs. JDBC ( A comparison)
A sample Hibernate Web Application using Servlets
Step by Step Hibernate - Your First Hibernate Application
Download Hibernate latest version
Contextual sessions in Hibernate
Fetching strategies in Hibernate
Programmatic configuration in Hibernate
Hi/lo algorithm in Hibernate
Hibernate Transaction Properties
Tuplizers (org.hibernate.tuple.Tuplizer) in Hibernate
Primary keys assigned by triggers in Hibernate
Types of inheritence models in Hibernate
Some Basic APIs in Hibernate
Creating Connection Pool for JDBC Connections in Hibernate
EntityNameResolvers in Hibernate
Most Emailed Articles (in Hibernate)
Hibernate Vs. JDBC ( A comparison)
Programmatic configuration in Hibernate
equals() and hashCode() in Hibernate
Step by Step Hibernate - Your First Hibernate Application
Delta Electronics Chairman Bruce Cheng Endows NCKU Build Y.S. Sun Green Building Research Center
Download Hibernate latest version
Contextual sessions in Hibernate
EntityNameResolvers in Hibernate
Hi/lo algorithm in Hibernate
Types of inheritence models in Hibernate
What is component mapping in hibernate?
Paging Through the Result Set in Hibernate
A sample Hibernate Web Application using Servlets
Some Basic APIs in Hibernate
SessionFactory in Hibernate