JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy
  • Contact Us

Pagination in Hibernate Query API

July 26, 2008 by Krishna Srinivasan Leave a Comment

Pagination in Hibernate Query API

Pagination is the very common problem for the most of the eneterprise applications.When we are retrieving thousands of records from the database, it is not good idea to retrieve all the records at the same time. So, we have to implement some sort of pagination concept in your application to restrict the number of rows to be fetched from the database.

also read:

  • Introduction to Hibernate
  • Hibernate Interview Questions
  • Interceptors in Hibernate
  • Hibernate Books

Pagination can be done in the middle tier or in the database itself. if you are implementing the pagination in themidd letier, you have to retrieve all the records from the database and store it in the cache. It will take more memory and slow down the performance. So, it is good idea to implement in the database. However, it purely depends on the project requirement.
Hibernate APIs provide few methods to set the pagination citerias in the query. We can use bothe normal Query and Criteria API. Look into the following code for how to create pagination using the Query object:
[code lang=”java”]
Query query = session.createQuery("from Studenet s");
query.setFirstResult(25);
query.setMaxResults(50);
[/code]
[code lang=”java”]
Criteria criteria = session.createCriteria(Student.class);
criteria.setFirstResult(25);
criteria.setMaxResults(50);
List result = criteria.list();
[/code]

In the above code, both the techniques are telling the Hibernate query for how many records to be retrieved and first set of records. These values can be dynamically changed and we can obtain the pagination implementation.

Filed Under: Hibernate Tagged With: pagination

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved