5. Hibernate - Criteria Queries

Hibernate - Criteria Queries
·         By Using this we can apply the conditions on Query’s
·         We have createCriteria(class) method in Session class to do this
Criteria cr = session.createCriteria(Employee.class);
cr.add(Restrictions.eq("salary", 2000));
List results = cr.list();

Examples :
Criteria cr = session.createCriteria(Employee.class);
 
// To get records having salary more than 2000
cr.add(Restrictions.gt("salary", 2000));
 
// To get records having salary less than 2000
cr.add(Restrictions.lt("salary", 2000));
 
// To get records having fistName starting with zara
cr.add(Restrictions.like("firstName", "zara%"));
 
// Case sensitive form of the above restriction.
cr.add(Restrictions.ilike("firstName", "zara%"));
 
// To get records having salary in between 1000 and 2000
cr.add(Restrictions.between("salary", 1000, 2000));
 
// To check if the given property is null
cr.add(Restrictions.isNull("salary"));
 
// To check if the given property is not null
cr.add(Restrictions.isNotNull("salary"));
 
// To check if the given property is empty
cr.add(Restrictions.isEmpty("salary"));
 
// To check if the given property is not empty
cr.add(Restrictions.isNotEmpty("salary"));

1. Criteria Interface              
·         List
·         uniqueObject


2. Restrictions Class
·         eq
·         gt
·         lt
·         between
·         distinct

3. Projections
·         min
·         max
·         avg