Hibernate Query Language(HQL)

Hibernate Query Language(HQL)

Hibernate Query Language (HQL) is a powerful query language. Being designed as an object-oriented extension to SQL bridges the gap between object-oriented systems and relational databases.
This language can also be used to retrieve objects from the database through O/R mapping by performing the following tasks:

  • Apply restrictions to properties of objects
  • Arrange the results returned by a query by using the order by clause
  • Paginate the results
  • Aggregate the records by using group by and having clauses
  • Use Joins
  • Create user-defined functions
  • Execute subqueries

Features of HQL

HQL supports associations and polymorphism, which in turn allows developers to write queries using less code than SQL. In addition, HQL supports many other SQL statements and aggregate functions, such as sum() and max() and clauses, such as group by and order by.

UPDATE clause

The UPDATE clause can be used to update one or more properties of one or more objects. Following is the example of UPDATE clause −

String hql = "UPDATE Employee set salary = :salary "  + 
             "WHERE id = :employee_id";
Query query = session.createQuery(hql);
query.setParameter("salary", 1000);
query.setParameter("employee_id", 10);
int result = query.executeUpdate();
System.out.println("Rows affected: " + result);

Bulk updates are new to HQL with Hibernate 3 and delete work differently in Hibernate 3 than in Hibernate 2. The Query interface now contains a method called executeUpdate() for executing HQL UPDATE or DELETE statements.