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(Expression.gt("roll", 5), Expression.like(
"name", "j%")));
List<Student> list = criteria.list();
Iterator iterator = list.iterator();
System.out.println("RollNo.\tName\tCourse");
while (iterator.hasNext()) {
Student student = (Student) iterator.next();
System.out.print(student.getRoll());
System.out.print("\t" + student.getName());
System.out.print("\t" + student.getCourse());
System.out.println();
}
}
}
Output :
Hibernate: select this_.roll_no as roll1_0_0_, this_.course as course0_0_, this_.name as name0_0_ from student this_ where (this_.roll_no>? or this_.name 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
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: Hibernate Expression
Post your Comment