Showing posts with label Hibernate Criteria Query examples. Show all posts
Showing posts with label Hibernate Criteria Query examples. Show all posts

Tuesday, 25 December 2018

Hibernate Criteria Query examples

Hibernate supports a feature that helps us to build queries dynamically. The org.hibernate.Criteria is the interface used for this purpose. Criteria is useful
when we are supposed to apply a multiple complicated search criteria on the queries.
For a Criteria query, a Criterion Object should be constructed (i.e) a call to createCriteria() passing the class of the objects we want the query to return.
Let us go the package com.demo.CriteriaExamples and execute the class CriteriaExampleExecutor.java
Criteria Example:
Following is an example that will all the instances of the class Employee.
Criteria criteria = session.createCriteria(Employee.class);
List critList = criteria.list();
for(Object objList: critList)
System.out.println("Employee Name is "+ ((Employee)objList).getEmpName());
Limiting the number of rows returned:
This is particularly useful in pagination.
Criteria criteria = session.createCriteria(Employee.class).setMaxResults(2);
critList = criteria.list();
System.out.println("Number of rows returned "+ critList.size());
for(Object objList: critList)
System.out.println("Employee Name is "+ ((Employee)objList).getEmpName());
Narrowing the result set:
Restrictions class provides factory methods for built-in Criterion types. In our example we have used some of the factory methods like Restrictions.like(),
Restrictions.between(), Restrictions.isNotNull().
Criteria criteria = session.createCriteria(Employee.class).add(Restrictions.like("empName", "Ana%"));
critList = criteria.list();
for(Object objList: critList)
System.out.println("Employee Name is "+ ((Employee)objList).getEmpName());
Ordering the Result Set:
The class Order provides the factoy methods asc() and desc() to order objects based on any particular property.
Criteria criteria = session.createCriteria(Employee.class).addOrder(Order.asc("empName"))
.addOrder(Order.desc("salary"));
critList = criteria.list();
for(Object objList: critList)
System.out.println("Employee Name is "+ ((Employee)objList).getEmpName() + " Employee Salary is " + ((Employee)objList).getSalary());
Logical and Comparison Operators available in Restrictions Class:
All regular SQL comparison operators are available in the Restrictions class. The methods Restrictions.or() and Restrictions.and() will serve the Logical
operators.
Criteria criteria = session.createCriteria(Employee.class).add(Restrictions.or(
Restrictions.gt("salary", new Integer(23000)), Restrictions.ge("age", new Integer(30))));
critList = criteria.list();
for(Object objList: critList)
System.out.println("Employee Name salary is more than 23000 or whose age is greater than or equal to 30 is "+ ((Employee)objList).getEmpName());
Aggregate Functions():
Projections class serves the factory methods which can be used as the aggregate functions as follows.
critList = session.createCriteria(Employee.class).setProjection(Projections.max("salary")).list();
System.out.println("Maximum salary is "+ critList.get(0));
critList = session.createCriteria(Employee.class).setProjection(Projections.min("salary")).list();
System.out.println("Minimum salary is "+ critList.get(0));
critList = session.createCriteria(Employee.class).setProjection(Projections.rowCount()).list();
System.out.println("Total employees "+ critList.get(0));
critList = session.createCriteria(Employee.class).setProjection(Projections.avg("age")).list();
System.out.println("Average employee age "+ critList.get(0));
critList = session.createCriteria(Employee.class).setProjection(Projections.sum("salary")).list();
System.out.println("Sum of all the salaries "+ critList.get(0));
Also, Projections class will help us to select exactly the objects or the properties of objects we need in the query result as follows.
session.createCriteria(Employee.class).setProjection(Projections.projectionList().add(Projections.id())
.add(Projections.property("empName"))
.add(Projections.property("age"))
);