HQL stands for Hibernate Query Language and is a fully object-oriented language. This language is a bit similar to the native query, but in the native SQL query, we use the physical table name and actual physical columns to execute a query, and in HQL, we have to use a class name instead of a table name and a field name instead of a column name.
HQL queries are converted to SQL queries by hibernate; so, we can use any of the styles. However, HQL is preferable from a performance point of view, because hibernate uses SQL directly without any optimization, and the HQL query uses hibernate's query generation strategy and caching mechanism.
There are multiple clauses available to work with HQL.
We will use the simple FROM
clause to query an object to load the complete object.
Here, we will use the FROM Category
query, which is equal to SELECT * FROM category
in native SQL. Execute the following code:
Query query = session.createQuery("FROM Category"); List<Category> list = query.list(); System.out.println("Category size: " + list.size());
Hibernate: select category0_.id as id1_, category0_.created_on as created2_1_, category0_.name as name1_ from category category0_ Category size: 2
From the output, it's clear that hibernate will execute a complete SELECT
statement for the Category
class for the FROM Category
query.
We can use multiple classes in the same query, which results in a Cartesian product or cross join.
Execute the following code:
Query query = session.createQuery("FroM Category, Product"); List list = query.list(); System.out.println("Result size: " + list.size());
Hibernate: select category0_.id as id1_0_, product1_.id as id0_1_, category0_.created_on as created2_1_0_, category0_.name as name1_0_, product1_.category_id as category4_0_1_, product1_.name as name0_1_, product1_.price as price0_1_ from category category0_ cross join product product1_ Result size: 12
Here, we used two classes: the first is Category
and the other is Product
. We have two records in the category
table and six records in the product
table, so the resulting size is 12
, which is equal to a Cartesian product (6 * 2 = 12). This query returns a List
of Object
array (List<Object[]>
).
We can use an alias to refer to this class in another part of the query, as shown in the following code:
String hql = "FROM Category c, Product p WHERE c.id=1";
The
FROM
clause used in the preceding section selects all the fields from a given class. SELECT
is used when we need limited columns.
Execute the following code:
Query query = session.createQuery("SELECT id, name from Category"); List list = query.list(); System.out.println("Result size: " + list.size());
When we use HQL, hibernate internally creates SQL. Hibernate uses the mapping provided via an HBM file if it is an XML-based mapping and uses annotations if it is an annotation-based mapping to convert HQL into a SQL query.
Hibernate uses a query generation strategy and caching mechanism in the HQL query. Query generation is used while converting HQL to SQL, and caching is used after the query execution is complete.
In this section, we will demonstrate the use of the FROM
and SELECT
clauses. But apart from these clauses, we can also use the other SQL clauses, as shown in the following examples: