Comparison operators in Hibernate

SHARE & COMMENT :

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

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());
        }
    }
}

also read:

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*

Close
Please support the site
By clicking any of these buttons you help our site to get better