Home Hibernate Examples Criteria Hibernate Criteria And Or

Ask Questions?

View Latest Questions


 
 

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

Hibernate Criteria And Or

The Hibernate Criteria And Or is same the the logical  "AND(&&)"  and  logical "Or(||)". To apply such type of condition based on logical 'AND' and logical 'Or' you can write as follows.

Criteria Or operation
Criteria criteria = session.createCriteria(Student.class);
	Criterion roll = Restrictions.eq("rollNo", 2);
	Criterion name = Restrictions.eq("name", "John");
	LogicalExpression expression = Restrictions.or(roll, name);
	criteria.add(expression);
	List list = criteria.list();

Criteria And operation
Criteria criteria = session.createCriteria(Student.class);
	Criterion roll = Restrictions.eq("rollNo", 1);
	Criterion name = Restrictions.eq("name", "John");
	LogicalExpression expression = Restrictions.and(roll, name);
	criteria.add(expression);
	List list = criteria.list();

Source Code for Criteria Or Application

OrCriteria.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.Criterion;
import org.hibernate.criterion.LogicalExpression;
import org.hibernate.criterion.Restrictions;

public class OrCriteria {

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

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

		Object object;

		try {
			// Creating a Criteria instance

			Criteria criteria = session.createCriteria(Student.class);
			Criterion roll = Restrictions.eq("rollNo", 2);
			Criterion name = Restrictions.eq("name", "John");
			LogicalExpression expression = Restrictions.or(roll, name);
			criteria.add(expression);
			List list = criteria.list();

			Iterator itr = list.iterator();
			System.out.println("\n");
			System.out.println("Student Details :- \n");
			if (list.size() == 0) {
				System.out.println("No Result Found !");
			}
			while (itr.hasNext()) {
				object = (Object) itr.next();
				System.out.printf("\t");
				System.out.println("First Name : "
						+ ((Student) object).getName());
			}

		} catch (HibernateException e) {

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

	}

}

Output of Above Code

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=? or this_.name=?)

Student Details :-

First Name : John
First Name : Raman

Source Code for Criteria And Application

AndCriteria.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.Criterion;
import org.hibernate.criterion.LogicalExpression;
import org.hibernate.criterion.Restrictions;

public class AndCriteria {

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

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

		Object object;

		try {
			// Creating a Criteria instance

			Criteria criteria = session.createCriteria(Student.class);
			Criterion roll = Restrictions.eq("rollNo", 1);
			Criterion name = Restrictions.eq("name", "John");
			LogicalExpression expression = Restrictions.and(roll, name);
			criteria.add(expression);
			List list = criteria.list();

			Iterator itr = list.iterator();
			System.out.println("\n");
			System.out.println("Student Details :- \n");
			if (list.size() == 0) {
				System.out.println("No Result Found !");
			}
			while (itr.hasNext()) {
				object = (Object) itr.next();
				System.out.printf("\t");
				System.out.println("First Name : "
						+ ((Student) object).getName());
			}

		} catch (HibernateException e) {

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

	}

}

Output of Above Code

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=? and this_.name=?)

Student Details :-

First Name : John

Download the Complete Source Code

Download Select Source Code

Related Tags for Hibernate Criteria And Or:


DMCA.com