Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Pagination using Hibernate Criteria

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 743
    Comment on it

    Hi friends ! I am writing this blog to guide you about pagination. Examples are written for MySQL and Hibernate framework to bring the data. This is not a limitation and you can use your own database and framework if you wish. The concept is almost similar with small changes in terminologies.

    Pagination plays a very important role. We can not load millions of records at the same time as there are certain limitation at the level of technology. To avoid such limitation we use database limits query. Hibernate provides us the method to use this MySQL query limit.

    In the following example, we will see how we can use the different techniques hibernate provided us to bring data in pages -

    Paging with HQL -
     

    Session session = sessionFactory.openSession();
    Query query = sess.createQuery("From Foo");
    query.setFirstResult(0);
    query.setMaxResults(10);
    List<Foo> fooList = fooList = query.list();

     

    Paging with Criteria API -

    Criteria criteria = session.createCriteria(Foo.class);
    criteria.setFirstResult(0);
    criteria.setMaxResults(pageSize);
    List<Foo> firstPage = criteria.list();

    Thanks. Happy coding.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: