Share on Google+Share on Google+

Hibernate Expression

In this section we will discuss Hibernate Expression.

Hibernate Expression

In this section we will discuss Hibernate Expression.

Hibernate Expression :

Expression is used by applications as a framework for creating new kind of criterion.
It is deprecated and you can use Restrictions in place of Expression. Expression class is defined in org.hibernate.criterion.Expression.
In general Expression is used for normal operation as eq, gt, lt, or, and etc. It provides to combine more than one conditions in to single expression.

Example :

In this example we are displaying record of such students whose roll number is greater than 5 or name starts with 'J' character.

package net.roseindia.application;

import java.util.Iterator;
import java.util.List;
import net.roseindia.table.Student;
import net.roseindia.util.HibernateUtil;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Expression;

public class HibernateExpression {
public static void main(String[] args) {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
Criteria criteria = session.createCriteria(Student.class);
criteria.add(Expression.or("roll", 5),
"name", "j%")));
List<Student> list = criteria.list();
Iterator iterator = list.iterator();
while (iterator.hasNext()) {
Student student = (Student);
System.out.print("\t" + student.getName());
System.out.print("\t" + student.getCourse());

Output :

Hibernate: select this_.roll_no as roll1_0_0_, this_.course as course0_0_, as name0_0_ from student this_ where (this_.roll_no>? or like ?)
RollNo.   Name     Course
4         Jenson   Hibernate
5         jacqub   Hibernate
6         Mandy    C
8         John     Hibernate
9         Linda    Hibernate

Click here to download complete source code


Posted on: July 28, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

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.

Discuss: Hibernate Expression  

Post your Comment

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