HQL (Hibernate Query Language) is Hibernate's own object-oriented query language, syntactically similar to SQL but operating on entity objects and their properties rather than tables and columns directly.
Key Points: • HQL queries reference entity class names and field names, not table and column names, making them database-portable. • It understands object-oriented concepts like inheritance and polymorphism — querying a superclass can return subclass instances too. • It supports joins across entity associations using dot notation or explicit join clauses, without needing to know the underlying foreign key columns. • Hibernate translates HQL into database-specific SQL at execution time based on the configured dialect. • JPQL (Java Persistence Query Language) is a closely related, JPA-standardized subset that most HQL knowledge transfers to directly.
Example: The query "from Employee e where e.salary > 50000" returns Employee entity instances directly, letting you write from Employee, not from a table name, and reference salary as a Java field rather than a SQL column.
Code Example:
Query<Employee> query = session.createQuery(
"from Employee e where e.department.name = :deptName", Employee.class);
query.setParameter("deptName", "Engineering");
List<Employee> results = query.list();Interview Tip: A concise interview answer is:
"HQL is Hibernate's object-oriented query language — it looks like SQL but operates on entity classes and their properties instead of tables and columns, understands inheritance and associations, and Hibernate compiles it down to database-specific SQL underneath."