How to use OR operator in hibernate Detached criteria query?

How to use OR operator in hibernate Detached criteria query?

How to use OR operator in hibernate Detached criteria query?

View Answers

June 1, 2012 at 5:45 PM

You can use OR operator in hibernate detachedCriteria through Restrictions.disjunction(). Detached Criteria query is a facility provide by Hibernate to write criteria queries in ?detached mode?, where hibernate session is not available. You can instantiate this class anywhere.


package net.roseindia.main;

import java.util.*;
import net.roseindia.table.Employee;
import net.roseindia.util.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;

public class DetachedCriteriaOR {
    public static void main(String[] args) {
        DetachedCriteria detachedCriteria = DetachedCriteria

        SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        List<Employee> employeeList = new ArrayList<Employee>();
        employeeList = detachedCriteria.getExecutableCriteria(session).list();
        Iterator it = employeeList.iterator();
        while (it.hasNext()) {
            Employee employee = (Employee);
            System.out.println("Name : " + employee.getName());
            System.out.println("Salary : " + employee.getSalary());
            System.out.println("Date of Join : " + employee.getDateOfJoin());



Hibernate: select this_.emp_id as emp1_0_0_, this_.date_of_join as date2_0_0_, as name0_0_, this_.salary as salary0_0_ from employee this_ where 1=1 and this_.emp_id<=? and like ?
Name : Ron
Salary : 10000
Date of Join : 2010-03-12 00:00:00.0
Name : Roxi
Salary : 220000
Date of Join : 2001-02-03 00:00:00.0

Description: In this example we are checking two conditions one is id must be less than equal to 4 and another one name must start with R character. Restrictions.disjunction() is used here for OR operator. Restrictions.disjunction() combinedly check both conditions and return result if any one expression is true.

Related Tutorials/Questions & Answers:
How to use AND operator in hibernate Detached criteria query?
How to use AND OR operator in hibernate Detached criteria query?
How to use OR operator in hibernate Detached criteria query?
.When to use detached criteria in hibernate?
Hibernate Detached Criteria Example
Hibernate Detached Criteria Subquery
Hibernate Detached Criteria Example
Hibernate Detached Criteria
Hibernate Criteria Detached Query And Subquery
AND operator in hibernate.
OR operator in hibernate.
Benefits of detached objects in hibernate
Hibernate criteria - how to use criteria to return only one element of an object instead the entire object.
Hibernate Criteria Detached Subquery Example
Hibernate criteria delete Example
Hibernate Criteria Detached Subquery Using "Subqueries"
Hibernate Criteria And Restrictions Example
Hibernate criteria average Example
Hibernate Criteria Between Example
Hibernate Criteria Wildcard Example
hibernate criteria disjunction Example
Criteria Query Examples in Hibernate
JPA CriteriaBuilder - How to use ?IN? comparison operator
Hibernate criteria by id.
How to get unique list in Hibernate using Criteria Query?
Hibernate Criteria Query Example Program
Hibernate Criteria Aggregate Functions Example
Hibernate Criteria Count Distinct Example
Hibernate Criteria Wildcard
Hibernate criteria for date. Example
Hibernate Criteria Examples
Criteria in Hibernate
Hibernate criteria disjunction.
Hibernate Criteria Max Date Example
Criteria with Multiple Tables - Hibernate
Hibernate Criteria Transformer Example
Hibernate Criteria Queries - Hibernate
Hibernate Criteria And Or Example
Hibernate Criteria Query Example
Hibernate Criteria
Hibernate Criteria Or
Hibernate-Criteria - Hibernate
Hibernate criteria by id.
Hibernate Criteria
Hibernate criteria date Example
Hibernate Criteria Count Example
Hibernate criteria count.
Hibernate Criteria With Collections Example
Using sum() in hibernate criteria.
What are criteria Queries in Hibernate?