Page 1 of 2 12 LastLast
Results 1 to 10 of 20

Thread: How do I implement pagination in Hibernate efficiently?

  1. #1
    Join Date
    Aug 2004
    Posts
    2

    Default How do I implement pagination in Hibernate efficiently?

    Hi,
    What is the best way to implement pagination of query results using Hibernate?

    I tried using the proxy parameter in the hibernate xml class definition files but proxies only work with load and invoke queries and getHibernateTemplate does not implement the invoke commands (only find), so what do I do? Can I use the same session as getHibernateTemplate and call the invoke command directly?



    Thanks in advance.

  2. #2
    Join Date
    Aug 2004
    Location
    Columbus, OH, USA
    Posts
    133

    Default

    Most examples I've seen use Hibernate's Query or Criteria APIs for pagination.

  3. #3
    Join Date
    Aug 2004
    Location
    Germany, Magdeburg
    Posts
    279

    Default

    Check the hibernate forum. Pagination is a hot topic over there. Another idea is to get the book: 'Hibernate in Action'. Great book!

  4. #4
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    I found this blog entry very informative. With some coding (take a look at HibernateTemplate), I am sure you could very easily implement Spring / Hibernate pagination helper methods
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  5. #5
    Join Date
    Aug 2004
    Location
    Germany, Magdeburg
    Posts
    279

    Default

    Using criteria the code looks like this:

    Criteria criteria=session.createCriteria(Item.class);
    criteria.addOrder(Order.asc("name"));
    criteria.setFirstResult(100);
    criteria.setMaxResults(50);
    List pageResults=criteria.list();

    Basically you just need to establish an order of the query results. (remember to ensure a distinct result set) and set the index of the first result and the number of maximum results. In the above sample you will receive the third page (each consisting of 50 items) sorted by names.

    Use the HibernateCallback interface for instance and use the HibnerateTemplate.execute(callback) method.

  6. #6
    Join Date
    Aug 2004
    Location
    St. Louis, MO
    Posts
    39

    Default

    Quote Originally Posted by irbouho
    I found this blog entry very informative.
    Perhaps a couple methods for pagination should be added to HibernateTemplate along with a Page helper class (as per the blog entry). It could probably be reworked to return Lists instead of the Page object, but I do like the additional functionality you could provide from such a Page. The addition to the api would be somthing along the lines of:

    Code:
    Page getPage(Query query, int pageSize, int pageNum) //this could also return a List
    List getAllPages(Query query, int pageSize) //returns a List of all the pages
    Thoughts?
    Ryan

  7. #7
    Join Date
    Aug 2004
    Posts
    2

    Default

    Hi,
    I found the Criteria class along with the HibernateCallback and Page wrapper (from blog entry) adequate for my needs.
    Thanks for the help!

    Should the Page wrapper class have a function
    int getNumPages(Query query, int pageSize)?

  8. #8
    Join Date
    Aug 2004
    Location
    St. Louis, MO
    Posts
    39

    Default

    Correct me if I'm wrong, but if you have the following code, wouldn't you lose the transactionality applied via Interceptor since the query is actually executed outside of the target class?

    Code:
    Page findStuffAsPage(int pageSize, int pageNum){
       return new Page(getHibernateTemplate().createQuery(getSession(), "from Cat c"), pageSize, pageNum);
    }
    Page Class from http://blog.hibernate.org/cgi-bin/bl.../14#pagination
    Code:
    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;
       }
    
    }


    R

  9. #9
    Join Date
    Aug 2004
    Location
    Germany, Magdeburg
    Posts
    279

    Default

    Correct me if I'm wrong, but if you have the following code, wouldn't you lose the transactionality applied via Interceptor since the query is actually executed outside of the target class?
    Depends on. As long as the transaction is spanning over the whole findStuffAsPage method, everything will be fine.
    For example:

    findStuffAsPage is advised and a declarative transaction is attached, the following fluff is likely to happen:

    <Transaction.begin>
    call createQuery
    create new Page
    return page
    <Transaction.end>

    The question remains, when the query is executed. As long as the query is executed during the creation of the Page object (!Bad style!), it will work fine. So I would prefer a factory method on that anyway.

    But I do not get what the really enhancement is the Page class will provide. You know setting first result and result count isn't a big deal anyway. Maybe adding an over all count + incooperate a table support would do it. But I don't know.... .

  10. #10
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    Just a small addition
    Page class implementation looks quiet simple, it should work fine as long as the whole call to findStuffAsPage is spanned in a Transaction. But you still need to pay attention to lazy intialization.
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

Similar Threads

  1. hibernate pagination
    By oliverchua in forum Data
    Replies: 8
    Last Post: Sep 23rd, 2005, 06:06 PM
  2. Loosing my SecureContext
    By sklakken in forum Security
    Replies: 3
    Last Post: Jul 21st, 2005, 01:44 PM
  3. Minimal Pagination w/o hibernate or struts
    By silly_me in forum Data
    Replies: 0
    Last Post: Jul 12th, 2005, 07:51 PM
  4. Replies: 3
    Last Post: Nov 19th, 2004, 07:16 PM
  5. Replies: 7
    Last Post: Aug 21st, 2004, 03:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •