Wednesday 24 August 2011

Hibernate Criteria Expression (eq)

In this section, you will learn to use the "eq" method. This is one of the most important method that is used to apply an "equal" constraint to the named property. 

Hibernate Criteria Expression (eq)

In this section, you will learn to use the "eq" method. This is one of the most important  method that is used to apply an "equal" constraint to the named property.
Expressions: The Hibernate Criteria API supports a rich set of comparison operators. Some standard SQL operators are =, <, ?, >, ?. That supports eq() method in Expression class.
In this tutorial, "Eq" is used with the date object. It takes two parameters e.g. eq("property_name",Object val).
Table Name: insurance









IDinsurance_nameinvested_amountinvestement_date
2Life Insurance250000000-00-00 00:00:00
1Givan Dhara20000 2007-07-30 17:29:05
3Life Insurance 5002005-10-15 00:00:00
4Car Insurance  25002005-01-01 00:00:00
5Dental Insurance5002004-01-01 00:00:00
Life Insurance9002003-01-01 00:00:00
Travel Insurance 2000 2005-02-02 00:00:00
Here is the code of the class using "eq" Expression :


package roseindia.tutorial.hibernate;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
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 HibernateCriteriaQueryExpressionEq {

  /**
 @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);
  DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
  Date date = 
(
Date)format.parse("2005-01-01 00:00:00");
  crit.add(Expression.eq("
investementDate"
,date));
  List list = crit.list();
  for(Iterator it =
list.iterator
();it.hasNext();){
  Insurance ins = 
(
Insurance)it.next();
  System.out.println("
Id: " 
+ ins.getLngInsuranceId());
  System.out.println("
Insurance Name: " 
+ ins.getInsuranceName());
  System.out.println("
Insurance Amount: " 
+ ins.getInvestementAmount());
  System.out.println("
Investement Date: " 
+ ins.getInvestementDate());
  }
  sess.clear();
  }
  catch(Exception e){
  System.out.println(e.getMessage());
  }
  }
}

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_.investement_date=?
Id: 4
Insurance Name: Car Insurance
Insurance Amount: 2500
Investement Date: 2005-01-01 00:00:00.0

No comments:

Post a Comment