About Hibernate Query Language


About Hibernate Query Language

Hibernate Query Language (HQL) is an easy-to-read and powerful query language designed as an object-oriented extension in SQL that closes the gap between object-based systems and interactive websites. The HQL syntax is very similar to the SQL syntax.

HQL Syntax

HQL queries are easy to understand and use persistent class words and structures instead of table names and columns. HQL contains the following features.

  • Clauses (FROM, Select, Where, Order by, Group by)
  • Associations and joins (Inner join, Left outer join, Right outer join, Full join)
  • Aggregate functions (avg, sum, min, max, count, etc.)
  • Expressions (Mathematical operators, Binary comparison, String concatenation, SQL scalar function, etc.)
  • Subqueries (any, all, some, in)

Order by clause

ORDER BY clause is used to sort the HQL query's results. You can order the results by any property on the object set, either ascending (ASC) or descending (DESC).

For example:

String hql = "FROM Employee E WHERE E.id > 10 ORDER BY E.salary DESC";
Query query = session.createQuery(hql);
List results = query.list();

If you wanted to filter by more than one layout, you could add additional features at the end of the order in a paragraph, separated by commas as follows -

String hql = "FROM Employee E WHERE E.id > 10 " +
             "ORDER BY E.firstName DESC, E.salary DESC ";
Query query = session.createQuery(hql);
List results = query.list();