- In Hibernate we pull the data from data base in two ways.
- session.get()/ session.load()
- Hibernate query langugae
- Now we will discuss about the third way Hibernate criteria query language which solves the problems of above two approaches.
Hibernate Criteria Query Language / HCQL
- In order to fetch the records based on some criteria we use Hibernate Criteria Query Language.
- We can make select operation on tables by applying some conditions by using HCQL.
- Criteria API is the alternative to HQL in object oriented approach.
- We can execute only SELECT statements using Criteria; we can’t execute UPDATE, DELETE statements using Criteria.
Advantages of Hibernate Criteria Query Language (HCQL)
- Criteria API allows us to define criteria query object by applying rules ,filtration and logical conditions
- So that we can add criteria to query.
- Criteria is also database independent, Because it internally generates HQL queries.
- Criteria is suitable for executing dynamic queries
- Criteria API also include Query by Example (QBE) functionality for supplying example objects.
- Criteria also includes projection and aggregation methods
Criteria Interface:
- Criteria Interface has some methods to specify criteria.
- Hibernate session interface has a method named createCriteria() to create criteria.
- public interface Criteria extends CriteriaSpecification
- Criteria criteria = session.createCriteria(Student.class);
- List<Student> studentList= criteria .list();
Methods of Criteria interface:
Order Class
- public class Order extends Object implements Serializable
- By using Order class we can sort the records in ascending or descending order.
- Order class provides two different methods to make ascending and descending.
- public static Order asc(String propertyName)
- public static Order desc(String propertyName)
Hibernate Criteria query example using order class
- Criteria criteria = session.createCriteria(Product.class)
- // To sort records in descening order
- criteria.addOrder(Order.desc("price"));
- // To sort records in ascending order
- criteria.addOrder(Order.asc("price"));
Restrictions Class
- public class Restrictions extends Object
- Restrictions class provides methods that can be used add restrictions (conditions) to criteria object.
- We have many methods in Restrictions class some of the commonly using methods are.
Hibernate Criteria query example using Restrictions class
- Criteria criteria = session.criteriaeateCriteria(Product.class);
- // To get records having price more than 3000
- criteria.add(Restrictions.gt("price", 3000));
- // To get records having price less than 2000
- criteria.add(Restrictions.lt("price", 2000));
- // To get records having productName starting with criteriaite
- criteria.add(Restrictions.like("productName", "criteriaite%"));
- // Case sensitive form of the above restriction.
- criteria.add(Restrictions.ilike("productName", "zara%"));
- // To get records having price in between 1000 and 2000
- criteria.add(Restrictions.between("price", 1000, 2000));
- // To check if the given property price is null
- criteria.add(Restrictions.isNull("price"));
- // To check if the given property is not null
- criteria.add(Restrictions.isNotNull("price"));
- // To check if the given property price is empty
- criteria.add(Restrictions.isEmpty("price"));
- // To check if the given property price is not empty
- criteria.add(Restrictions.isNotEmpty("price"));
- List results = criteria.list();
Pagination using Hibernate Criteria
- By using criteria methods setFirstResult() and setMaxResults() we can achieve pagination concept.
- Criteria criteria = session.createCriteria(Product.class);
- criteria.setMaxResults(10);
- criteria.setFirstResult(20);
Projections class in Hibernate
- public final class Projections extends Object
- By Using org.hibernate.criterion.Projections class methods we can perform operations like minimum , maximum, average , sum and count.
Hibernate Criteria query example using Projection class
- Criteria criteria = session.criteriaeateCriteria(Product.class);
- // To get total row count.
- criteria.setProjection(Projections.rowCount());
- // To get average price.
- criteria.setProjection(Projections.avg("price"));
- // To get distinct countof name
- criteria.setProjection(Projections.countDistinct("name"));
- // To get maximum price
- criteria.setProjection(Projections.max("price"));
- // To get minimum price
- criteria.setProjection(Projections.min("price"));
- // To get sum of price
- criteria.setProjection(Projections.sum("price"))
No comments