Hibernate Restrictions with Example

In this tutorials, we are going to learn about Hibernate Restrictions with Example. In the previous tutorial we had a detailed discussion about Hibernate Projections and how to use them. Coming to the Hibernate Restrictions, if we want to apply a condition in criteria query, then we can use Restrictions.

Bullet points about Hibernate Restrictions :

If we want to apply a condition to criteria query, we need Criterion object.

Criterion is an interface, it is an object-oriented representation of a query criterion that may be used as a restriction in a Criteria query.

We can obtain the Criterion type objects, by calling static methods of Restrictions class.

Restrictions is a normal Java class, which doesn’t implement any Criterion interface.

Criterion interface and Restrictions class, both are coming from org.hibernate.criterion package.

Hibernate Restrictions Example :

If we want to read employees, who are working in department 101, then a condition can be added like the following:

In the above example, we are trying to get the employees information, who are working in department id = 101 and salary > 4000. This is exactly like select * from emp where deptid=101 and salary > 4000 in SQL. Restrictions class has and() method to apply the ‘and condition’ in a query. We can add variable number of conditions in and() method like and(criterion,criterion-2,criterion-3,…..,criterion-n);

Hi
Folks,
you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,