
What is criteria disjunction in hibernate?

In Hibernate, Criteria disjunction works as logical OR operator. disjunction() groups the expressions into a single expression. If any one condition is satisfied, it will return true with its result as output. It returns false if all conditions are not satisfied. Take a look of bellow example- Steps-
1)Copy library (jar file) of hibernate into your lib folder.
2)Create hibernate configuration file (hibernate.cfg.xml) and save it in the same folder where you are going to save your source code.
3)Now create Persistent class Employee.java?Hibernate uses the Plain Old Java Object (POJO) classes to map to the database table using annotations.
4)Next create an util class as HibernateUtil.java
5)Here is your MainClass-
package net.roseindia.main;
import java.util.*;
import net.roseindia.table.Employee;
import net.roseindia.util.ConnectionUtil;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
public class HibernateConjunction{
public static void main(String []args){
SessionFactory sessionFactory = ConnectionUtil.getSessionFactory();
Session session = sessionFactory.openSession();
Criteria criteria = session.createCriteria(Employee.class);
criteria.add(Restrictions.disjunction().add(Restrictions.eq("id",2))
.add(Restrictions.like("name", "R%"))
);
List<Employee> employeeList = new ArrayList<Employee>();
employeeList = criteria.list();
Iterator it = employeeList.iterator();
while (it.hasNext()) {
Employee employee = (Employee) it.next();
System.out.println(employee.getName());
}
session.close();
}
}
Output :
Hibernate: select this_.emp_id as emp1_0_0_, this_.date_of_join as date2_0_0_, this_.name as name0_0_, this_.salary as salary0_0_ from employee this_ where (this_.emp_id=? or this_.name like ?) Ron Som Roxi
Description: -Employee is table class mapped with employee table. In the example, there are two conditions to check. First id must be equal to 2 or name must be started with 'r' character. Restrictions.disjunction() combinedly check both conditions and return result if any one expression is true.