Comparison operators in Hibernate

July 26, 2008

Hibernate

Comparison operators

HQL supports all the operators used in the SQL language. But, Criteria API doesn’t support the arithmetic expressions. Apart from that, it is easy to use other operators in the Criteria API itself. This tips provides few basic example programs on using the operators.

JavaBeatHibernateExample.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package javabeat.net.hibernate;
 
import java.math.BigDecimal;
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;
 
/**
 * source : www.javabeat.net
 */
public class JavaBeatHibernateExample {
    public static void main(String args[]) {
        Configuration configuration = new Configuration();
        // configuring hibernate
        SessionFactory sessionFactory = configuration.configure().buildSessionFactory();
        Session session = sessionFactory.openSession();
        Criteria criteria = session.createCriteria(Student.class);
        criteria.add(Expression.between("id",new Long(0),new Long(10)));
        //criteria.add(Expression.gt("id",new Long(1)));//great than
        //criteria.add(Expression.ge("id",new Long(1)));//greater than or equal
        //criteria.add(Expression.lt("id",new Long(1)));//less than
        //criteria.add(Expression.le("id",new Long(1)));//less than or equal
        //criteria.add(Expression.eq("id",new Long(0)));//equal
        //criteria.add(Expression.in("name",new String[]{"test"}));//equal
        List<Student> list = criteria.list();
        for (Student student : list){
            System.out.println(student.getName());
        }
    }
}
 
</code>
email

Comments

comments

Related posts:

  1. NULL and NOT NULL comparison in the Hibernate API
  2. Hibernate Criteria Query Example
  3. Three ways to create query in Hibernate
  4. Batch insert in Hibernate
  5. How to Configure hibernate using XML files?

No comments yet.

Leave a Reply