As we know hibernate criteria is a very important aspect of Hibernate framework as it simplifies the way to fetch tabular data from DB. In this article, we will be discussing about different examples on hibernate criteria. Starting from what is criteria, we will look into how to construct criterias to perform common read operations that we do by using query langages such as joins, sorting, sub queries etc.
To fetch data from relational DB, we use different query Languages such as HQL or SQL but the common thing here is we use certain query language which is again a new set of language that relational DB understands. Criteria deals with this restrictions by providing a simplified API for retrieving entities by composing Criterion objects.
It is very ideal and elegant for creating dyamic queries and simple to add ordering, restrictions and paginations while fetching data from hibernate supported databases.
Though it completly depends upon the requirements and projects type whether you should use criteria over HQL or SQL, following are some points that support hibernate criteria.
1. Criteria is safe from vulnerable to SQL injection as queries are fixed or parameterized.
2. HQL is ideal for static queries.
3. HQL does not support pagination but it can be achieved by Criteria.
4. Criteria helps us to build queries in a cleaner way and most of our errors are solved during compile time.
However Crtieria also lags in some use cases. For example HQL can perform both select and non-select operations. Criteria can only select data, you can not perform non-select operations using criteria queries. In case of performance Criteria is slower than HQL.
Lets take an example using two entities
Publisher.java and fetch desired records using criteria.
A basic criteria query looks like this
It fetches all the records from Book table and the equivalent sql query is follow
In the above example * is used to represent all the column names.As we are using JPA specifcation EAGER Type is by default provided for ManyToOne relationship. so we are getting a
LEFT OUTER JOIN with Publisher entity.
Following is an example to apply restrictions in criteria
This is equivalent to the following SQL query:
Now lets add more filter using AND ,OR using restrictions.
This is equivalent to the following SQL:
Similiarly we can use Restrictions.gt(..) ,Restrictions.lt(..), Restrictions.isEmpty(..) ,Restrictions.isNotEmpty(..),Restrictions.isNull(..) ,Restrictions.between(..)
We can add the sql functions like min(), max(), avg() etc using criteria. Here is the criteria to find a hibernate book of least price
The generated sql query will be:
You can order the results using org.hibernate.criterion.Order.
The equivalent sql query will look as :
By navigating associations using createCriteria() you can specify constraints upon related entities.lets write criteria to fetch books with name containing'Hibernate' of publishers with name containing 'Apress'
The second criteria used above creates a new criteria instance representing publisher instance.The generated sql query is
To demonstrate this example instead of creating an separate entity lets change the fetch type for publisher to LAZY in Book entity and acheive the eager fetching via criteria.
Lets fetch it using criteria.
This fetches book along with corresponding publisher if present.The equivalent sql output is
Lets retain the same LAZY relationship as above to avoid the
LEFT OUTER JOIN with Publisher entity and compose a subquery.
We are going to use DetachedCriteria for sub query.DetachedCriteria class allows us to create a query outside the scope of a session and then execute it using an arbitrary Session.
The generated SQL query is:
I hope this article served you whatever you were looking for. If you have anything that you want to add or share then please share it below in the comment section.
3. Spring Boot Mvc App With Jsp
Contact us: firstname.lastname@example.org