Hibernate Query Language (HQL)

 Hibernate Query Language (HQL) is an object-oriented query language similar to SQL but operates on Hibernate entities instead of database tables. Here's an example of using HQL to query the database.


Step 1: Entity Class

import jakarta.persistence.*; @Entity @Table(name = "employee") public class Employee { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; @Column(name = "name") private String name; @Column(name = "department") private String department; @Column(name = "salary") private double salary; // Getters and setters public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDepartment() { return department; } public void setDepartment(String department) { this.department = department; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } }

Step 2: HQL Query Examples

Example 1: Fetch All Employees

import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import java.util.List; public class HQLExample { public static void main(String[] args) { SessionFactory sessionFactory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory(); Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); try { // HQL Query String hql = "FROM Employee"; List<Employee> employees = session.createQuery(hql, Employee.class).getResultList(); for (Employee emp : employees) { System.out.println("ID: " + emp.getId() + ", Name: " + emp.getName() + ", Department: " + emp.getDepartment() + ", Salary: " + emp.getSalary()); } transaction.commit(); } catch (Exception e) { if (transaction != null) transaction.rollback(); e.printStackTrace(); } finally { session.close(); sessionFactory.close(); } } }

Example 2: Fetch Employees by Department

String hql = "FROM Employee WHERE department = :dept"; List<Employee> employees = session.createQuery(hql, Employee.class) .setParameter("dept", "HR") .getResultList();

Example 3: Fetch Employees with Salary Greater Than a Threshold

String hql = "FROM Employee WHERE salary > :minSalary"; List<Employee> employees = session.createQuery(hql, Employee.class) .setParameter("minSalary", 50000.0) .getResultList();

Example 4: Fetch Employees with Multiple Conditions

String hql = "FROM Employee WHERE department = :dept AND salary > :minSalary"; List<Employee> employees = session.createQuery(hql, Employee.class) .setParameter("dept", "IT") .setParameter("minSalary", 60000.0) .getResultList();

Example 5: Sorting Results

String hql = "FROM Employee ORDER BY name ASC"; List<Employee> employees = session.createQuery(hql, Employee.class).getResultList();

Example 6: Aggregation (Count, Max, etc.)

// Count the number of employees String hql = "SELECT COUNT(e) FROM Employee e"; Long count = session.createQuery(hql, Long.class).getSingleResult(); System.out.println("Total Employees: " + count); // Get maximum salary hql = "SELECT MAX(e.salary) FROM Employee e"; Double maxSalary = session.createQuery(hql, Double.class).getSingleResult(); System.out.println("Max Salary: " + maxSalary);

Example 7: Pagination


String hql = "FROM Employee"; List<Employee> employees = session.createQuery(hql, Employee.class) .setFirstResult(0) // Start position .setMaxResults(10) // Number of records .getResultList();

Comments