Comparison operators in 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.


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 :
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("id",new Long(1)));//great than
        //criteria.add("id",new Long(1)));//greater than or equal
        //criteria.add("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("name",new String[]{"test"}));//equal
        List<Student> list = criteria.list();
        for (Student student : list){

also read:

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Share This

Share this post with your friends!