Criteria Query In Hibernate
Posted By : Dharmendra Kumar | 31-May-2018
Sometimes it's very important to apply dynamic search to our data so that we can get the more restricted recordset.
To apply these restrictions Hibernate provides a very efficient way i.e. criteria query.
1. To use criteria queries in your project first you have to create the bean of HibernateJpaSessionFactoryBean class.
@Bean
public HibernateJpaSessionFactoryBean sessionFactory() {
HibernateJpaSessionFactoryBean hibernateJpaSessionFactoryBean = null;
try{
hibernateJpaSessionFactoryBean = new HibernateJpaSessionFactoryBean();
}catch(Exception e){
logger.debug("Exception occure while create HibernateJpaSessionFactoryBean : "+e);
}
logger.debug("HibernateJpaSessionFactoryBean created succesfully.");
return hibernateJpaSessionFactoryBean;
}
2. Now let's suppose we want to apply dynamic search on Employee domain :
@Entity
class Employee{
int eId;
String name;
Date dateOfJoining;
Integer salary;
//getters and setters
}
3. Now create a method in your employee service class where we'll fetch the records from database dynamically and also
@Autowired
private HibernateJpaSessionFactoryBean sessionFactory;
4. Now create an object of Criteria class from sessionFactory as
Criteria criteria = session.createCriteria(Employee.class);
5. Now we can call methods of Criteria class to apply restrictions in our dynamic query as per requirement.
add() method of Criteria class can be used to apply restrictions and,to apply pagination setFirstResult() and setMaxResults() can be used.
We can also apply sorting on fetched record set by using addOrder(Order.desc("salary")) or addOrder(Order.asc("salary")).
//this will return records with salary greater than 2000
cr.add(Restrictions.gt("salary", 0000));
//this will return records with salary less than 2000
criteria.add(Restrictions.lt("salary", 10000));
//this will return records with name starting with John
criteria.add(Restrictions.like("name", "John%"));
// To get records having dateOfJoining between 2004/05/21 and 2008/01/12
criteria.add(Restrictions.between("dateOfJoining", "2004/05/21", "2008/01/12"));
//To apply pagination by setting page number 1 and page size as 10
criteria.setFirstResult(1);
criteria.setMaxResults(10);
// To sort records in descening order
criteria.addOrder(Order.desc("salary"));
// To sort records in ascending order
criteria.addOrder(Order.asc("dateOfJoining"));
6. After applying all these restrictions our dynamic query with multiple restrictions is ready.
Now we have to fetch the records from the database on the basis of this query.
We can do this by calling list() method on the criteria object.
List<Employee> employeeList=criteria.list();
Now employeeList contains records according to our search restrictions.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Dharmendra Kumar
Dharmendra is skilled in Java, Spring MVC, JPA, MySQL, and C.He has Bachelor's degree focussed in Computer Science and Engineering.