In this tutorial, you will see the use of Restrictions class in java. It is used to restrict the retrieval of data from database.

Syntax:

Criteria criteria = session.createCriteria(Model.class);
criteria.add(Restrictions.eq(propertyName, propertyValue));

 

Criteria restrictions query

 

The Restrictions class provide many methods to do the comparison operation.

Restrictions.eq

This is used to apply an “equal” constraint to the named property.
Simple SQL query >>
SELECT * FROM student WHERE rollNumber = 3;
Above query in Criteria API >>
Criteria criteria = session.createCriteria(Student.class);
criteria.add(Restrictions.eq("rollNumber", 3));
List list = criteria.list();

 

Restrictions.lt

This is used to apply a “less than” constraint to the named property.
Simple SQL query >>
SELECT * FROM student WHERE rollNumber < 3;
Above query in Criteria API >>
Criteria criteria = session.createCriteria(Student.class);
criteria.add(Restrictions.lt("rollNumber", 3));
List list = criteria.list();

Restrictions.gt

This is used to apply a “greater than” constraint to the named property.
Simple SQL query >>
SELECT * FROM student WHERE rollNumber > 3;
Above query in Criteria API >>
Criteria criteria = session.createCriteria(Student.class);
criteria.add(Restrictions.gt("rollNumber", 3));
List list = criteria.list();

Restrictions.le

This is used to apply a “less than or equal” constraint to the named property.
Simple SQL query >>
SELECT * FROM student WHERE rollNumber <= 3;
Above query in Criteria API >>
Criteria criteria = session.createCriteria(Student.class);
criteria.add(Restrictions.le("rollNumber", 3));
List list = criteria.list();

Restrictions.ge

This is used to apply a “greater than or equal” constraint to the named property.
Simple SQL query >>
SELECT * FROM student WHERE rollNumber >= 3;
Above query in Criteria API >>
Criteria criteria = session.createCriteria(Student.class);
criteria.add(Restrictions.ge("rollNumber", 3));
List list = criteria.list();

Restrictions.ne

This is used to apply a “not equal” constraint to the named property.
Simple SQL query >>
SELECT * FROM student WHERE rollNumber <> 3;
Above query in Criteria API >>
Criteria criteria = session.createCriteria(Student.class);
criteria.add(Restrictions.ne("rollNumber", 3));
List list = criteria.list();

Restrictions.in

This is used to apply an “in” constraint to the named property.
Simple SQL query >>
SELECT * FROM student WHERE rollNumber IN (3,5,7);
Above query in Criteria API >>
Criteria criteria = session.createCriteria(Student.class);
criteria.add(Restrictions.in("rollNumber", new Integer[]{3,5,7}));
List list = criteria.list();

Restrictions.or

This returns the disjunction of two expressions.
Simple SQL query >>
SELECT * FROM student WHERE rollNumber > 3 OR course='MA';
Above query in Criteria API >>
Criteria criteria = session.createCriteria(Student.class);
criteria.add(Restrictions.or(Restrictions.gt("rollNumber", 3), Restrictions.eq("course", "MA"));
List list = criteria.list();

Restrictions.not

This returns the negation of an expression.
Simple SQL query >>
SELECT * FROM student WHERE rollNumber NOT IN (3,5,7);
Above query in Criteria API >>
Criteria criteria = session.createCriteria(Student.class);
criteria.add(Restrictions.not(Restrictions.in("rollNumber", new Integer[]{3,5,7})));
List list = criteria.list();

Restrictions.like

This is used to apply a “like” constraint to the named property.
Simple SQL query >>
SELECT * FROM student WHERE studentName LIKE %RAJPUT%;
Above query in Criteria API >>
Criteria criteria = session.createCriteria(Student.class);
criteria.add(Restrictions.like("studentName", "%RAJPUT%"));
List list = criteria.list();

Restrictions.isNull

This is used to apply an “is null” constraint to the named property.
Simple SQL query >>
SELECT * FROM student WHERE studentName IS NULL;
Above query in Criteria API >>
Criteria criteria = session.createCriteria(Student.class);
criteria.add(Restrictions.isNull("studentName"));
List list = criteria.list();

Restrictions.isNotNull

This is used to apply an “is not null” constraint to the named property.
Simple SQL query >>
SELECT * FROM student WHERE studentName IS NOT NULL;
Above query in Criteria API >>
Criteria criteria = session.createCriteria(Student.class);
criteria.add(Restrictions.isNotNull("studentName"));
List list = criteria.list();

Restriction.between

This is used to apply a “between” constraint to the named property.
Simple SQL query >>
SELECT * FROM student WHERE rollNumber BETWEEN (3,7);
Above query in Criteria API >>
Criteria criteria = session.createCriteria(Student.class);
criteria.add(Restrictions.between("rollNumber", 3, 7));
List list = criteria.list();

Restriction.allEq

This is used to apply an “equals” constraint to each property in the key set of a Map.
Simple SQL query >>
SELECT * FROM user WHERE userName = +userName AND userPassword = +userPassword;
Above query in Criteria API >>
Map map = new HashMap();  
map.put("username", username);  
map.put("userPassword", userPassword);  
Criteria criteria = session.createCriteria(User.class);
criteria.add(Restrictions.allEq(map));
List list = criteria.uniqueResult();

In the Next Chapter we will discuss more about the Cacheing in Hibernate.

<<Previous Chapter 30<<    >>Next Chapter 32>>