Home Hibernate Examples Criteria Restrictions Hibernate Criteria Multiple or

Ask Questions?

View Latest Questions


 
 

Hibernate Criteria Multiple or
Posted on: April 14, 2011 at 12:00 AM
Advertisement
In this tutorial you will learn about hibernate criteria multiple or

Hibernate Criteria Multiple or

You can implements multiple or in hibernate criteria
Criteria criteria = session.createCriteria(Student.class);
	Criterion address = Restrictions.like("address", "patna",
			MatchMode.ANYWHERE);
	Criterion firstName = Restrictions.eq("name", "Ramesh");
	Disjunction disjunction = Restrictions.disjunction();
	disjunction.add(address);
	disjunction.add(firstName);
	criteria.add(disjunction);
	List list = criteria.list();

An example of multiple or is given below

CriteriaMultipleOr.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;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

public class CriteriaMultipleOr {
	public static void main(String[] args) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Criteria criteria = session.createCriteria(Student.class);
		Criterion address = Restrictions.like("address", "patna",
				MatchMode.ANYWHERE);
		Criterion firstName = Restrictions.eq("name", "Ramesh");
		Disjunction disjunction = Restrictions.disjunction();
		disjunction.add(address);
		disjunction.add(firstName);
		criteria.add(disjunction);
		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_, this_.name as name0_0_, this_.course as course0_0_, this_.address as address0_0_, this_.addmision_date as addmision5_0_0_ from student this_ where (this_.address like ? or this_.name=?)
Ramesh
Vikash
Ramesh
Chulbul Panday

Download Complete Source Code

Related Tags for Hibernate Criteria Multiple or:


DMCA.com