: Techiey Dreamz :

Sunday, February 12, 2006

Pagination in Hibernate !


Pagination in Hibernate and EJB3

by gavin@hibernate.org | Link

Pagination, I want to display "next" and "previous" buttons to the user, but disable them if there are no more, or no previous query results. But I don't want to retrieve all the query results in each request, or execute a separate query to count them. So, here's the correct approach:

public class Page {      private List results;   private int pageSize;   private int page;      public Page(Query query, int page, int pageSize) {              this.page = page;       this.pageSize = pageSize;       results = query.setFirstResult(page * pageSize)           .setMaxResults(pageSize+1)           .list();      }      public boolean isNextPage() {       return results.size() > pageSize;   }      public boolean isPreviousPage() {       return page > 0;   }      public List getList() {       return isNextPage() ?           results.subList(0, pageSize-1) :           results;   }}        

You can return this object to your JSP, and use it in Struts, WebWork or JSTL tags. Getting a page in your persistence logic is as simple as:

public Page getPosts(int page) {    return new Page(         session.createQuery("from Posts p order by p.date desc")        page,        40    );}        

The Page class works in both Hibernate and EJB 3.0.

Related links :

Check-out the discussion about ScrollableResults over paging here.

0 Comments:

Post a Comment

<< Home