Hibernate Criteria Expression (or)

In this section, you will learn to use the "or" method. This is one of the most important method that returns the disjunction of the two expressions.


Hibernate 5 JPA Configuration Hibernate 5 persistence.xml example Hibernate 5 JPA Tutorial Hibernate and JPA Example How properties of a class are mapped to the columns of a database table in Hibernate? Hibernate 5 Annotations Maven dependency Hibernate 5 annotationconfiguration Doctype Hibernate-configuration 5 Hibernate 5 Dialect Hibernate 5 configuration dtd Hibernate 5 Hello World Getting started with Hibernate 5 Hibernate 5.2.10 jar Download Hibernate 5.2.10 Maven Dependency Hibernate 5 Maven Dependency Hibernate 5 native query example Hibernate 5 query deprecated When should you use Hibernate? What's new in Hibernate 5? How much time it takes to learn Hibernate? Hibernate envers custom revinfo table Hibernate 5 Envers Hibernate Envers get all revisions Hibernate Examples Hibernate 5 build SessionFactory Example Hibernate 5 SessionFactory Example Hibernate 5 Annotation Example How to download Hibernate 5.x? Hibernate 5 Features Hibernate 5 Tutorials Learning Hibernate ORM - Free Training/Tutorial videos of Hibernate ORM with Java How to write hello world example program in Hibernate? How to create one to one relationship in Hibernate 4 with Annotation? How to calculate minimum in Hibernate using the min() Function Hibernate ORM 4.2.21.Final Released - Create CURD application in latest Hibernate Hibernate ORM 4.2.21 Tutorial - Learn Hibernate ORM Framework Hibernate Error: Failed to create sessionFactory object.java.lang.NoClassDefFoundError How to create Hibernate Project in Eclipse? Examples of Hibernate Criteria Query Hibernate ORM 4.3.6.Final released

Hibernate Criteria Expression (or)


In this section, you will learn to use the "or" method. This is one of the most important  method that returns the disjunction of the two expressions. You can also build the nested expressions using 'and' and 'or'. 
The Hibernate Criteria API supports a rich set of comparison operators. Some standard SQL operators are =, <, ?, >, ?. 

Expression or(Criterion LHS, Criterion RHS): This method returns the disjuction of two expressions. Any given condition is 'true' then it executes the query. In this tutorial, "or" is used :

Expression.or(Expression.eq("lngInsuranceId", new Long(3), Expression.eq("IngInsuranceId", new Long(6))).

Table Name: insurance

ID insurance_name invested_amount investement_date
2 Life Insurance 25000 0000-00-00 00:00:00
1 Givan Dhara 20000  2007-07-30 17:29:05
3 Life Insurance  500 2005-10-15 00:00:00
4 Car Insurance   2500 2005-01-01 00:00:00
5 Dental Insurance 500 2004-01-01 00:00:00
Life Insurance 900 2003-01-01 00:00:00
Travel Insurance  2000  2005-02-02 00:00:00

Here is the code of the class using "or" Expression :

package roseindia.tutorial.hibernate;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Expression;

public class HibernateCriteriaQueryExpressionOr {

 @param args
  public static void main(String[] args) {
  // TODO Auto-generated method stub

  Session sess = null;
  try {
  SessionFactory fact = new 
  sess = fact.openSession();
  Criteria crit = sess.
Expression.eq("lngInsuranceId",new Long(3)),
("lngInsuranceId",new Long(6))));
List list = crit.list();
 for (Iterator it = list.iterator();it.hasNext();){
Insurance ins = (Insurance)it.next();
Id: " 
+ ins.getLngInsuranceId());
Name: "  
+ ins.getInsuranceName());
Amount: " 
+ ins.getInvestementAmount());
+ ins.getInvestementDate());
  catch(Exception e){

Download this Code.


log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).

log4j:WARN Please initialize the log4j system properly.

Hibernate: select this_.ID as ID0_0_, this_.insurance_name as insurance2_0_0_, this_.invested_amount as invested3_0_0_, this_.investement_date as investem4_0_0_ from insurance this_ where (this_.ID=? or this_.ID=?)

Insurance Id: 3

Insurance Name: Life Insurance

Invested Amount: 500

Investement Date:2005-10-15 00:00:00.0

Insurance Id: 6

Insurance Name: Life Insurance

Invested Amount: 900

Investement Date:2003-01-01 00:00:00.0

Share on Google+Share on Google+

Hibernate Criteria Expression (or)

Posted on: February 27, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Hibernate Criteria Expression (or)   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image