Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Hibernate Hibernate Criteria Expression (and)

 
 

Share on Google+Share on Google+

Hibernate Criteria Expression (and)

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

Hibernate Criteria Expression (and)

     

In this section, you will learn to use the "and" method. This is one of the most important  method that returns the conjunctions of 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 and(Criterion LHS, Criterion RHS): This method returns the conjunctions of two expressions. Both conditions are 'true' then it xecutes the query otherwise not. In this tutorial, "and" is used :

Expression.and(Expression.gt("lngInsuranceId", new Long(3), Expression.lt("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 "and" 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 HibernateCriteriaQueryExpressionAnd {

  /**
 @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.and
(Expression.gt("lngInsuranceId",new Long(3)),
 Expression.lt
("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>? and this_.ID<?)

Insurance Id: 4

Insurance Name: Car Insurance

Invested Amount: 2500

Investement Date: 2005-01-01 00:00:00.0

Insurance Id: 5

Insurance Name: Dental Insurance

Invested Amount: 500

Investement Date: 2004-01-01 00:00:00.0

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 27, 2008

Related Tutorials

Discuss: Hibernate Criteria Expression (and)   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
PAN
June 19, 2012
can i add one more condition inside expression

crit.add(Expression.and (Expression.gt("lngInsuranceId",new Long(3)), Expression.lt("lngInsuranceId",new Long(6)))); can i add one more expression inside the and(.gt(),.lt(),.eq());
DMCA.com