Hibernate Criteria Limit
Posted on: April 9, 2011 at 12:00 AM
In this tutorial you will learn about the hibernate criteria Limit

Hibernate Criteria Limit

There may be situation where you may want some limited no of records from databases. In hibernate you can set the limit of records to be fetched easily as

Criteria criteria = session.createCriteria(Student.class);
	criteria.setMaxResults(4);
	List list=criteria.list();

The Above statement will fetch 4 records

 An Example of Criteria Limit is given below, please consider the exampleADS_TO_REPLACE_1

CriteriaLimit.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 CriteriaLimit {
	public static void main(String[] args) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Criteria criteria = session.createCriteria(Student.class);
		criteria.setMaxResults(4);
		List list = criteria.list();
		Iterator iterator = list.iterator();
		while (iterator.hasNext()) {
			Student student = (Student) iterator.next();
			System.out.println(student.getName());
		}
	}
}

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

Hibernate: select this_.roll_no as roll1_0_0_, this_.name as name0_0_, this_.course as course0_0_, this_.address as address0_0_ from student this_ limit ?
Ramesh
Raman
Aman
Raman

Download Complete Source Code

Related Tags for Hibernate Criteria Limit:

Advertisements

Ads

 
Advertisement null

Ads