Home Hibernate Examples Criteria Hibernate Criteria Pagination

Ask Questions?

View Latest Questions


 
 

Hibernate Criteria Pagination
Posted on: April 12, 2011 at 12:00 AM
Advertisement
In this tutorial you will learn about Hibernate Criteria Pagination

Hibernate Criteria Pagination

You can easily make a pagination based application in Hibernate Criteria. You need to do the things that set first result and maximum result as,

criteria.setFirstResult(start);
criteria.setMaxResults(end);

An example of hibernate criteria pagination is given below

CriteriaPagination.java

package net.roseindia.main;

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

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

import org.hibernate.Criteria;
import org.hibernate.Session;

public class CriteriaPagination {
	public static void main(String[] args) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Criteria criteria = session.createCriteria(Student.class);
		int start = 0;
		int end = 5;
		criteria.setFirstResult(start);
		criteria.setMaxResults(end);
		List result = criteria.list();
		Iterator iterator = result.iterator();
		System.out.println("\n");
		System.out.print("\tRoll No");
		System.out.print("\t");
		System.out.print("\tName");
		System.out.print("\t");
		System.out.print("\tCourse");
		System.out.print("\t");
		System.out.print("\tAddress");
		System.out.print("\t");
		System.out.println();
		while (iterator.hasNext()) {
			System.out.println("");
			Student student = (Student) iterator.next();
			System.out.print("\t" + student.getRollNo() + "\t|\t"
					+ student.getName() + "\t|\t" + student.getCourse()
					+ "\t|\t" + student.getAddress() + "\t\t");
		}
	}
}

When you run this application it will display message as shown below:

Hibernate: select this_.roll_no as roll1_0_, this_.name as name0_0_, this_.course as course0_0_, this_.address as address0_0_ from student this_ limit ?


Roll No Name Course Address

1 | Ramesh | MCA | Delhi
2 | Raman | B.Tec | Lucknow
3 | Aman | BCA | Gorakhpur
4 | Raman | MCA | Amritsar
5 | Vikash | MBA | Patna

Download Complete Source Code

Related Tags for Hibernate Criteria Pagination:


DMCA.com