Hibernate Tutorial

Hibernate Create Criteria Restrictions Example

Hibernate Create Criteria Restrictions

static Criterion between(String propertyName, Object lo, Object hi) 
          Apply a "between" constraint to the named property
  c.add(Restrictions.between("age", 20, 50));

static SimpleExpression eq(String propertyName, Object value) 
          Apply an "equal" constraint to the named property
  c.add(Restrictions.eq("name", "Candidjava"));

static SimpleExpression ge(String propertyName, Object value) 
          Apply a "greater than or equal" constraint to the named property
  c.add(Restrictions.ge("salary", new Integer(30000)));

static SimpleExpression gt(String propertyName, Object value) 
          Apply a "greater than" constraint to the named property
  c.add(Restrictions.gt("salary", new Integer(30000)));

static Criterion in(String propertyName, Object[] values) 
          Apply an "in" constraint to the named property
  c.add(Restrictions.in("city", new String[] { "Chennai", "Erode" }));

static SimpleExpression le(String propertyName, Object value) 
          Apply a "less than or equal" constraint to the named property
  c.add(Restrictions.le("salary", new Integer(90000)));

static SimpleExpression like(String propertyName, Object value) 
          Apply a "like" constraint to the named property
  c.add(Restrictions.like("name", "Can%");

static SimpleExpression lt(String propertyName, Object value) 
          Apply a "less than" constraint to the named property
  c.add(Restrictions.lt("salary", new Integer(90000)));
           
static SimpleExpression ne(String propertyName, Object value) 
          Apply a "not equal" constraint to the named property
  c.add(Restrictions.le("city", ?Delhi?);

Example

public List<Employee> search(){
    List<Employee> li = null;
    try {
      Session s = getSession();
 
      Criteria c = s.createCriteria(Employee.class);
 
      c.add(Restrictions.between("age", 20, 50));
  
      c.add(Restrictions.ge("salary", new Integer(30000)));
      
      c.add(Restrictions.le("salary", new Integer(50000)));
      
      c.add(Restrictions.in("city", new String[] { "Chennai", "Erode" }));
      
      c.add(Restrictions.eq("gender", "MALE"));
 
      li = c.list();
    } catch (HibernateException e) {
      System.out.println(e.getMessage());
    }
    return li;
  }

POJO Class

package com.candidjava.hibernate;
public class Employee {
  private int id;  
  private String name;
  private int age;
  private int salary;
  private String city;
  private String gender;
    
  public int getId() {  
      return id;  
  }  
  public void setId(int id) {  
      this.id = id;  
  }  
  public String getName() {  
      return name;  
  }  
  public void setName(String name) {  
      this.name = name;  
  }
  public int getAge() {  
      return age;  
  }  
  public void setAge(int age) {  
      this.age = age;  
  }  
  public int getSalary() {  
      return salary;  
  }  
  public void setSalary(int salary) {  
      this.salary = salary;  
  }  
  public String getCity() {  
      return city;  
  }  
  public void setCity(String city) {  
      this.city = city;  
  }
  public String getGender() {  
      return gender;  
  }  
  public void setGender(String gender) {  
      this.gender = gender;  
  }
}

Download

Hibernate Restrictions example war

Hibernate Restrictions example zip