Introducing HQL: The Object-Oriented Query Language from Hibernate
In the previous article, we explored Hibernate, a popular Open Source O/R Mapping framework for J2EE-based enterprise applications. Hibernate automates to a large extent the creation of an efficient persistence layer for the enterprise application. Hibernate makes mapping objects to be persisted to underlying database easier. In other words, Hibernate allows representing an underlying database by using simple Java objects and vice versa.
By facilitating direct retrieval of persistent objects from the database, Hibernate automates/hides the process of creating objects and populating them with data retrieved from the database (common in JDBC-based applications), saving the developer from such tedious routine tasks.
Hibernate uses the following ways to retrieve objects from the database:
- Hibernate Query Language (HQL)
- Query By Criteria (QBC) and Query BY Example (QBE) using Criteria API
- Native SQL queries
The most preferred way is using the Hibernate Query Language (HQL), which is an easy-to-learn and powerful query language, designed as a minimal object-oriented extension to SQL. HQL has syntax and keywords/clauses very similar to SQL. It also supports many other SQL-like features, such as aggregate functions (for example: sum(), max()) and clauses such as group by and order by clause.
Note: The Criteria API also uses HQL behind the scenes.
Although it is possible to use native SQL queries directly with a Hibernate-based persistence layer, it is more efficient to use HQL instead. The reasons of choosing HQL over the other two methods are given below:
- HQL allows representing SQL queries in object-oriented terms—by using objects and properties of objects.
- Instead of returning plain data, HQL queries return the query result(s) in the form of object(s)/tuples of object(s) that are ready to be accessed, operated upon, and manipulated programmatically. This approach does away with the routine task of creating and populating objects from scratch with the "resultset" retrieved from database queried.
- HQL fully supports polymorphic queries. That is, along with the object to be returned as a query result, all child objects (objects of subclasses) of the given object shall be returned.
- HQL is easy to learn and implement, as its syntax and features are very similar to SQL.
- HQL contains many advance features such as pagination, fetch join with dynamic profiling, and so forth, as compared to SQL.
- HQL facilitates writing database-type independent queries that are converted to the native SQL dialect of the underlying database at runtime. This approach helps tap the extra features the native SQL query provides, without using a non-standard native SQL query.
As described earlier, most of HQL's syntax and features are very similar to SQL. An HQL query may consist of following elements:
- Aggregate functions
Clauses are HQL keywords that make up queries. The following table contains some important and commonly/often used clauses and their syntax along with their examples.
Table A: HQL Clauses with their description, syntax, and examples.
|from||The simplest form of an HQL query. Specifies the object whose instances are to be returned as the query result. Commonly used with the select clause.||from object [as object_alias]* object_alias simply means another name given to refer to an object for convenience.||from comp.Dept as dept|
Will return all instances of object dept.
|select||Specifies objects and properties to be returned in the query result set. Used in conjunction with the from clause.||select [object.]property||select dept.mgr from comp.Dept as dept|
Will return all values of mgr in all instances of dept.
|where||Specifies the conditions that should be satisfied by the instances returned as the query result. Used with select and/or from clause.||where condition
Here, condition is a combination of logical, relational operators i.e. =, >, AND, NOT etc.
|select dept.mgr from comp.Dept as dept where dept.emp_no > 10|
Will return all instances of mgr in dept whose corresponding emp_no values are greater than 10.
|order by||Specifies the order (ascending/descending) in which the properties of objects returned as query results should be listed. Used with the select and from clauses.||order by object0.property0 [asc|desc][, object1.property0]...
By default, order is ascending unless specified otherwise.
|select dept.mgr from comp.Dept as dept order by dept.emp_no asc|
Will return a list of all instances of mgr in dept in ascending order of corresponding emp_no values.
|group by||Specifies the grouping criteria using objects properties, by which the list of objects returned as a query result should be grouped together. Used with the select and/or from clause.||group by object0.property0[, object1.property0]...||select dept.emp_no from comp.Dept as dept group by dept.mgr|
Will return list of all emp_no instances from dept grouped by corresponding values of mgr.
|Bold: keywords/clauses||Italics: variables|
|[...]: Optional||a | b: either a or b|
Page 1 of 2