Home Hibernate Examples Criteria Hibernate criteria setMaxResult.

Ask Questions?

View Latest Questions


 
 

Hibernate criteria setMaxResult.
Posted on: April 11, 2011 at 12:00 AM
Advertisement
Hibernate criteria setMaxResult.

Hibernate criteria setMaxResult.

In this tutorial, you will see the use of setMaxResult() method of criteria class. It specifies to total number of result which will be access.

Syntax :

Criteria criteria=session.createCriteria(Pojo.class);
criteria.addOrder(Order.desc(propertyName));

LazyLoading.java

package net.roseindia.action;

import java.util.Iterator;
import java.util.List;
import net.roseindia.bean.StudentBean;
import net.roseindia.util.HibernateUtil;
import org.hibernate.Criteria;
import org.hibernate.Session;

public class CriteiaSort {
	public static void main(String[] args) {
		Session session = HibernateUtil.getSessionFactory().openSession();

		Criteria criteria = session.createCriteria(StudentBean.class);
		criteria.setMaxResults(2);
		List list = criteria.list();
		Iterator studentIter = list.iterator();
		System.out.println("***************************************");
		System.out.println("   Roll No      Name      Address   ");
		System.out.println("****************************************");
		while (studentIter.hasNext()) {
			StudentBean student = (StudentBean) studentIter.next();
			System.out.print("\t" + student.getRoll() + "\t");
			System.out.print(student.getName() + "\t");
			System.out.print(student.getAddress() + "\n");
		}
		System.out.println("**************************************");
	}
}
When you run this application it will display message as shown below:
Hibernate: select this_.id as id0_0_, this_.name as name0_0_, this_.class as class0_0_, this_.address as address0_0_, this_.email as email0_0_, this_.joindate as joindate0_0_ from studentinfo this_ limit ?
***************************************
Roll No     Name       Address
****************************************
1            bharat        Bareilly
2           gyan            Bareilly
**************************************

Download Complete Source Code

Related Tags for Hibernate criteria setMaxResult.:


DMCA.com