Hibernate Criteria Queries

Hibernate Criteria Queries provide a flexible and type-safe way to construct database queries in Java. This approach is particularly useful when you need to dynamically build complex queries based on various criteria.

Key Concepts:

  • Criteria Interface: The core interface for building criteria queries. It provides methods to add restrictions, orderings, projections, and other query parameters.
  • Restrictions: These are used to specify conditions on the query, such as equality, inequality, like, between, and more.
  • Orderings: These are used to sort the results of the query.
  • Projections: These are used to select specific columns or aggregate functions from the results.

Hibernate Criteria Query Example

The Criteria API is used for programmatic query building. Here’s how to query employees based on certain criteria:

Example 1: Fetch All Employees

import org.hibernate.Criteria; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import java.util.List; public class CriteriaExample { public static void main(String[] args) { // Create SessionFactory SessionFactory sessionFactory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory(); // Open Session Session session = sessionFactory.openSession(); // Begin Transaction Transaction transaction = session.beginTransaction(); try { // Create Criteria Criteria criteria = session.createCriteria(Employee.class); // Fetch all employees List<Employee> employees = criteria.list(); for (Employee emp : employees) { System.out.println("ID: " + emp.getId() + ", Name: " + emp.getName() + ", Department: " + emp.getDepartment() + ", Salary: " + emp.getSalary()); } // Commit Transaction transaction.commit(); } catch (Exception e) { if (transaction != null) transaction.rollback(); e.printStackTrace(); } finally { session.close(); sessionFactory.close(); } } }

Example 2: Fetch Employees with a Salary Greater than a Threshold

import org.hibernate.criterion.Restrictions; Criteria criteria = session.createCriteria(Employee.class); criteria.add(Restrictions.gt("salary", 50000.0)); // Salary > 50000 List<Employee> employees = criteria.list();

Example 3: Fetch Employees Belonging to a Specific Department

criteria.add(Restrictions.eq("department", "HR")); // Department = HR List<Employee> employees = criteria.list();

Example 4: Sorting Results

import org.hibernate.criterion.Order; criteria.addOrder(Order.asc("name")); // Sort by name in ascending order List<Employee> employees = criteria.list();

Example 5: Combining Multiple Conditions

criteria.add(Restrictions.and( Restrictions.eq("department", "IT"), // Department = IT Restrictions.gt("salary", 60000.0) // Salary > 60000 )); List<Employee> employees = criteria.list();

Key Points:

  1. Criteria is an older approach and is part of Hibernate 5 and earlier.
  2. For dynamic queries, the Criteria API provides a clean, object-oriented alternative to HQL.
  3. Hibernate Criteria API has been deprecated in favor of the JPA Criteria API in newer versions of Hibernate (Hibernate 6+).

Comments