Hibernate criteria disjunction.

Hibernate criteria disjunction.

What is criteria disjunction in hibernate?

View Answers

May 31, 2012 at 11:38 AM

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);
            .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();

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 ?)

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.


Related Tutorials/Questions & Answers:
Hibernate criteria disjunction.
Hibernate criteria disjunction.  What is criteria disjunction in hibernate
hibernate criteria disjunction Example
hibernate criteria disjunction Example  I want example of hibernate criteria disjunction. Thanks   Example of hibernate criteria disjunction. Please check the tutorial hibernate criteria disjunction. This tutorial
Hibernate Disjunction.
This section contains explanation of Hibernate disjunction with example
hibernate criteria disjunction
hibernate criteria Disjunction In this Example, We will discuss about hibernate criteria query, In this example we create a criteria instance and implement.... In This example search the result according to disjunction. The disjunction
Hibernate Criteria Examples
Hibernate Criteria Disjunction Hibernate Criteria With Collections Hibernate...Hibernate Criteria Examples In this section we are giving many example of using Hibernate Criteria query. With the help of criteria query you can
Hibernate Criteria Multiple or
Hibernate Criteria Multiple or You can implements multiple or in hibernate criteria Criteria criteria = session.createCriteria(Student.class); Criterion...); Criterion firstName = Restrictions.eq("name", "Ramesh"); Disjunction disjunction
Examples of Hibernate Criteria Query
hibernate criteria delete hibernate criteria disjunction Hibernate Criteria...Example code and running projects of Hibernate Criteria Query Here are examples of Criteria Query in Hibernate and all the examples comes with the running
Hibernate Criteria And Or Example
Hibernate Criteria And Or Example  Hibernate Criteria And Or Example I want example of Hibernate Criteria And Or in Java Thanks   Example of Hibernate Criteria And Or Criteria Check the tutorial Hibernate Criteria
Hibernate Criteria
In this tutorial we are going to discuss Hibernate Criteria with example
Hibernate Criteria Or
In this section we are going to discuss Hibernate Criteria Or with example
Hibernate criteria by id.
Hibernate criteria by id.  How to display record by using criteria by id in Hibernate
Hibernate criteria delete Example
Hibernate criteria delete Example  How to use the delete criteria in Hibernate? thanks
Hibernate Criteria Count Example
Hibernate Criteria Count Example  Example of Hibernate Criteria Count. Check the tutorial Hibernate Criteria Count. Check the Latest tutorials, articles and examples of Hibernate Framework. Thanks
Hibernate criteria date Example
Hibernate criteria date Example  Example of Hibernate criteria date. Check the tutorial hibernate criteria date. Check the Latest tutorials, articles and examples of Hibernate Framework. Thanks
Hibernate Criteria With Collections Example
Hibernate Criteria With Collections Example  Good code of Hibernate... Hibernate Criteria With Collections in your Java project. Check the tutorial Hibernate Criteria With Collections . Check the Latest tutorials, articles
Hibernate Criteria And Restrictions Example
Hibernate Criteria And Restrictions Example  How to use Criteria And Restrictions in Hibernate? Thanks   I will provide you the example code of Hibernate Criteria And Restrictions. Check the tutorial Hibernate
Hibernate Criteria Wildcard
Hibernate Criteria Wildcard   How to write Criteria Query in Hibernate that uses the Wildcards?   Hello, Check the example Hibernate Criteria Wildcard. Thanks
What are criteria Queries in Hibernate?
What are criteria Queries in Hibernate?  Just explain me what is Criteria Query in Hibernate with examples? Thanks   Criteria queries are very helpful in hibernate for queries based on certain criteria. Check
What is Hibernate Criteria Transformer?
What is Hibernate Criteria Transformer?  Hi, Can anyone explain me about the Hibernate Criteria Transformer? I need good example of Hibernate Criteria Transformer . Thanks   Hi, check the example Hibernate Criteria
Hibernate criteria for date. Example
Hibernate criteria for date. Example  What to use the Hibernate criteria for date in Hibernate based application? I want example of Hibernate criteria for date in Hibernate orm. Thanks   Example of Hibernate criteria
Hibernate criteria average Example
Hibernate criteria average Example   How to find the average in Hibernate? Thanks   Example of finding average on a filed in Hibernate... the tutorial Hibernate criteria avg.. Check the Latest tutorials, articles
Hibernate Criteria Between Example
Hibernate Criteria Between Example  How to use the Between in the Hibernate Query? Share me good example code. Thanks   Example of Hibernate Criteria Between. Check the tutorial Hibernate Criteria Between. Check
Hibernate Detached Criteria Example
Hibernate Detached Criteria Example  How to use Hibernate Detached Criteria in Java program? What is the use of Hibernate Detached Criteria? Explain... Detached Criteria. Check Latest Hibernate Tutorials and Articles
Hibernate Criteria Wildcard Example
Hibernate Criteria Wildcard Example  Hi, How to write Java program to use the Hibernate Criteria Wildcard query and fetch the data from database I want example of Hibernate Criteria Wildcard which is easy to understand
Hibernate Detached Criteria Example
Hibernate Detached Criteria Example  can anyone share me the source code of using detached Criteria in Hibernate? Thanks   Hi, If you are looking for the Example program of using Hibernate Detached Criteria
Criteria Query Examples in Hibernate
Criteria Query Examples in Hibernate  How to use the Criteria Query in Hibernate? How to select the data using Criteria Query of Hibernate? Thanks... Criteria Query in Hibernate with Examples code. Check the tutorial Criteria Query
Hibernate Criteria Transformer Example
Hibernate Criteria Transformer Example  I want example of Hibernate...   Here I am giving you the link of Hibernate Criteria Transforme example program. Check the tutorial Hibernate Criteria Transformer . Check the Latest
Hibernate Criteria Query Example
Hibernate Criteria Query Example  Hi, How to create Hibernate Criteria Query Examples in Java program? Share me the easy to learn example code. Thanks   Hi, Hibernate Criteria Query is used to selectively find
Hibernate Detached Criteria
This section is good discussion about hibernate detached criteria
Hibernate criteria count.
Hibernate criteria count.  How do we count rows using criteria in hibernate?   We can get the rows count using the Projections. My code is like this: Criteria criteria = session.createCriteria(getReferenceClass
Hibernate criteria query
Hibernate criteria query  Hi... I need criteria query like query... a.countryId=c.Chrono and a.groupId=b.Chrono and a.type =1 "); like this in hibernate criteria example. Thanks in advance.   Have a look at the following link