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)