Sobes.tech
Junior

What is HQL (Hibernate Query Language)?

sobes.tech AI

Answer from AI

HQL is an object-oriented query language developed within Hibernate for querying data in a relational database, using Java domain model objects instead of database tables and columns.

Main features of HQL:

  • Object-oriented: Queries operate with entity class names and their properties, not SQL tables and columns.
  • Database dialect independence: HQL abstracts the details of the specific SQL dialect used by Hibernate.
  • Support for polymorphism: HQL can query descendant classes in an inheritance hierarchy.
  • Support for named parameters: Allows safer passing of values into queries, preventing SQL injection.
  • Ability to perform DML operations: Supports INSERT, UPDATE, DELETE.

Example of an HQL query:

// Select all users with the name "John Doe"
String hql = "FROM User WHERE name = :userName";
Query query = session.createQuery(hql);
query.setParameter("userName", "John Doe");
List<User> users = query.list();