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.javaADS_TO_REPLACE_1
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"); } } }
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 |
Advertisements
Ads
Ads