How to achieve Pagination in Hibernate Query language (HQL) in Hibernate

In my project for achieve pagination i have used below HQL 

There are two methods of the Query interface for pagination.


1 Query setFirstResult(int startPosition)
This method takes an integer that represents the first row in your result set, 
starting with row 0.

2 Query setMaxResults(int maxResult)
This method tells Hibernate to retrieve a fixed number maxResults of objects.
Using above two methods together, we can construct a paging component in our web
 or Swing application. Following is the example which you can extend to fetch 10 
rows at a time:

String hql = "FROM Employee";
Query query = session.createQuery(hql);
query.setFirstResult(1);
query.setMaxResults(10);
List results = query.list();

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.