Frameworks| Hibernate| Struts| JSF| JavaFX| Ajax| Spring| DOJO| JDO| iBatis| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
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 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'. 
Expressions:
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 
Configuration
().configure().buildSessionFactory();
      sess = fact.openSession();
      Criteria crit = sess.
createCriteria
(Insurance.class);
      crit.add(Expression.or
(
Expression.eq("lngInsuranceId",new Long(3)),
 Expression.eq
("lngInsuranceId",new Long(6))));
List list = crit.list();
 for (Iterator it = list.iterator();it.hasNext();){
Insurance ins = (Insurance)it.next();
 System.out.println("Insurance 
Id: " 
+ ins.getLngInsuranceId());
System.out.println("Insurance 
Name: "  
+ ins.getInsuranceName());
System.out.println("Invested 
Amount: " 
+ ins.getInvestementAmount());
System.out.println("Investement 
Date:" 
+ ins.getInvestementDate());
      }
      sess.close();
    }
    catch(Exception e){
      System.out.println(e.getMessage());
    }
  }
}

Download this Code.

Output:

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

                         

» View all related tutorials
Related Tags: sql c hibernate api com class ant comparison import io property method sed const operators expression operator port criteria name

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

3 comments so far (
post your own) View All Comments Latest 10 Comments:

What i should do in case when it's need to use more then two OR ?
like:
WHERE
firstname LIKE ".." OR
secondname LIKE ".." OR
thirdname LIKE ".." OR
description LIKE ".."


reply:

you can do somthing like this:

search = "%"+search+"%";
Criteria criteria = session.createCriteria(your.class);
criteria.add(
Expression.or(
Expression.or(
Expression.ilike("colum", search),
Expression.ilike("columx", search)
),
Expression.or(
Expression.or(
Expression.ilike("columx+1", search),
Expression.ilike("columx+2", search)
),
Expression.or(
Expression.ilike("columx+3", search),
Expression.ilike("columx+4", search)
)
)
)
);

this is not a realy good way of doing it but it works.

Posted by sdf on Saturday, 11.15.08 @ 00:09am | #81685

What i should do in case when it's need to use more then two OR ?
like:
WHERE
firstname LIKE ".." OR
secondname LIKE ".." OR
thirdname LIKE ".." OR
description LIKE ".."

Posted by iBog on Friday, 09.12.08 @ 15:18pm | #78875

Hi,
I have a problem in writing a criteria expression for nested properties.
Say your "insurance" table has a column "brokerid" and it's the primary key in table "insurance_broker". That table has fields for name, address, etc.
And assume those tables are properly mapped in mapping files.

I want to write something like

Expression.or
(Expression.eq("insurance_name",new Long(3)),
Expression.eq("insurance_broker.name","Test Broker")));

Thanks in advance

Posted by Chamara Ranasinghe on Friday, 01.11.08 @ 13:12pm | #44968

Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.