Hibernate Pagination

In this tutorial we will discuss concept of pagination in hibernate.

Tutorials   
Hibernate 5 native query example Hibernate 5 query deprecated When should you use Hibernate? What's new in Hibernate 5? How much time it takes to learn Hibernate? Hibernate envers custom revinfo table Hibernate 5 Envers Hibernate Envers get all revisions Hibernate Examples Hibernate 5 build SessionFactory Example Hibernate 5 SessionFactory Example Hibernate 5 Annotation Example How to download Hibernate 5.x? Hibernate 5 Features Hibernate 5 Tutorials Learning Hibernate ORM - Free Training/Tutorial videos of Hibernate ORM with Java How to write hello world example program in Hibernate? How to create one to one relationship in Hibernate 4 with Annotation? How to calculate minimum in Hibernate using the min() Function Hibernate ORM 4.2.21.Final Released - Create CURD application in latest Hibernate Hibernate ORM 4.2.21 Tutorial - Learn Hibernate ORM Framework Hibernate Error: Failed to create sessionFactory object.java.lang.NoClassDefFoundError How to create Hibernate Project in Eclipse? Examples of Hibernate Criteria Query Hibernate ORM 4.3.6.Final released Hibernate ORM 4.2.14.Final released Hibernate 4.3.4 Released: Hibernate 4.3.4.Final Maven dependency Hibernate association and join example Learn Hibernate programming with Examples Introduction to Hibernate Framework Hibernate Tutorial: Learn Hibernate with examples Hibernate 4.3 Hello World: Example Hibernate ORM 4.3.1.Final released How to create SessionFactory in Hibernate 4.3.1? Hibernate 4 Hello World: Example of Hello World program Hibernate 4 create Session Factory: Example of creating Session Factory in Hibernate 4 Hibernate 4.3 and JPA 2.1 Example What is Hibernate in Java? Hibernate Overview and Architecture What is JPA 2.1?

Ads

Ads

Hibernate Pagination

In this tutorial we will discuss concept of pagination in hibernate.

Pagination in Hibernate :

Pagination is used where you have to display large amount of result. It is tough to display all the record into a single page.
For convenient and better display hibernate provides concept of pagination. In pagination large result set is divided into a number of pages and at a time you can fetch one page.
For this, just set start position and page size. Use "order by" clause to sort result dataset.

Code :

String hql = "SELECT stud.roll, stud.name, stud.course FROM Student stud";
Query query = session.createQuery(hql);
query.setFirstResult(start);
query.setMaxResults(end);

setFirstResult(int arg0) : sets the starting point of your table record. It is contained in Query org.hibernate.Query.setFirstResult(int arg0)
It takes one int type argument.
setMaxResults():  sets the maximum result into your single page. It is defined in Query org.hibernate.Query.setFirstResult(int arg0).
It takes one int type argument.

Example : Here we are selecting record of student and displaying it by using pagination concept.

main class code :

package net.roseindia.main;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.roseindia.table.Student;
import net.roseindia.util.HibernateUtil;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class MainClazz {

public static void main(String[] args) {
Session session = HibernateUtil.getSessionFactory().openSession();
int start = 0;
int end = 5;
String hql = "SELECT stud.roll, stud.name, stud.course FROM Student stud order by stud.roll";
Query query = session.createQuery(hql);
query.setFirstResult(start);
query.setMaxResults(end);
query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);

List objectList = query.list();
Iterator iterator = objectList.iterator();
System.out.println("RollNo.\t Name \t Course");
while (iterator.hasNext()) {
Map map = (Map) iterator.next();
System.out.print(map.get("0"));
System.out.print("\t" + map.get("1"));
System.out.print("\t" + map.get("2"));
System.out.println();
}

session.close(); // session is closed
}

}

Output :

Hibernate: select student0_.roll_no as col_0_0_, student0_.name as col_1_0_, student0_.course as col_2_0_ from student student0_ limit ?
RollNo.  Name    Course
1        Rondy   java
3        Johny   unix
4        Roxi    Hibernate
5        Johny   Hibernate
6        johny   C

Click here to download complete source code

Advertisements

Ads
Share on Google+Share on Google+

Hibernate Pagination

Posted on: July 23, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Hibernate Pagination  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
Kumar Madhukar
September 19, 2012
Hibernate not support jdbc driver for Oracle 9i

Hi, today, i face a error hibernate not support Oracle thin Driver for 9i.

Ads

 

Ads