Home Hibernate Examples Criteria Hibernate Criteria And Restrictions

Ask Questions?

View Latest Questions


 
 

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

Hibernate Criteria Restriction

The Hibernate Criteria API contains Restriction class. By using this class you can add restriction on your query.

Example- Suppose fetch the student record whose roll number is 1. Then you can use Restriction class and write query as

Criteria criteriaEaquals = session.createCriteria(Student.class);
	criteriaEaquals.add(Restrictions.eq("rollNo", 1));
	List list = criteriaEaquals.list();

This will fetch the record of the student whose roll no is 1.

The Restriction class have so many methods such as eq(), in(), gt(), lt() etc.

An example of Hibernate Criteria Restriction class is given below

CriteriaRestriction.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.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

public class CriteriaRestriction {

	@SuppressWarnings("unchecked")
	public static void main(String[] args) {

		Session session = HibernateUtil.getSessionFactory().openSession();

		Object object;

		try {
			// Creating a Criteria instance

			Criteria criteriaEaquals = session.createCriteria(Student.class);
			System.out.println("\n");

			// Criteria Equals Restriction
			criteriaEaquals.add(Restrictions.eq("rollNo", 2));
			List list = criteriaEaquals.list();
			Iterator itr = list.iterator();
			if (list.size() == 0) {
				System.out.println("No Result Found !");
			}
			while (itr.hasNext()) {
				object = (Object) itr.next();
				System.out.printf("\t");
				System.out.println(((Student) object).getRollNo() + "\t"
						+ ((Student) object).getName() + "\t"
						+ ((Student) object).getCourse() + "\t"
						+ ((Student) object).getAddress());
			}

			// Criteria Greater Than Restriction

			Criteria criteriaGreatherThan = session
					.createCriteria(Student.class);
			System.out.println("\n");
			criteriaGreatherThan.add(Restrictions.gt("rollNo", 2));
			List listGListGreaterThan = criteriaGreatherThan.list();
			Iterator itrGreaterThan = listGListGreaterThan.iterator();
			if (list.size() == 0) {
				System.out.println("No Result Found !");
			}
			while (itrGreaterThan.hasNext()) {
				object = (Object) itrGreaterThan.next();
				System.out.printf("\t");
				System.out.println(((Student) object).getRollNo() + "\t"
						+ ((Student) object).getName() + "\t"
						+ ((Student) object).getCourse() + "\t"
						+ ((Student) object).getAddress());
			}
		} catch (HibernateException e) {

			e.printStackTrace();
		} finally {
			session.close();
		}

	}
}

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_ where this_.roll_no=?
2 Raman B.Tec Lucknow


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_ where this_.roll_no>?
3 Aman BCA Gorakhpur
4 Mahesh MCA Amritsar
5 Vikash MBA Patna
6 Ramesh DCA Gwalior

Download Complete Source Code

Related Tags for Hibernate Criteria And Restrictions:


DMCA.com