Criterion and Restrictions



How to use criterion ?
Criteria criteria = session.createCriteria(Entity.class);
Criterion criterion = Restrictions. gt (" id ", new Integer(11));

// .gt() : means greater than
// id is a variable of our POJO class Entity.java

criteria.add(criterion); // adding criterion object to criteria class object
List l = criteria.list(); // executing criteria query

In our above example we are fetching the data by comparing id greater than (>) 11
If we want to put more conditions on the data (multiple conditions) then we can use .and() method ,. or() method , .eq() method using the Restrictions class

How to use Restrictions?
Criteria criteria = session.createCriteria(Entity.class);
criteria.add(Restrictions.and ( Restrictions.like("name","%M%"),Restrictions.eq ( "category",new String(“bird”) )) );

List list = criteria.list();
Iterator it = list.iterator();

Restrictions.like("name","%M%") : this will add condition for a name which contains the M aphabet.

Restrictions.eq ( "category",new String(“bird”) )) : this will add condition for a category. It should be bird.