Executing a query using HQL

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.

How to do it…

There are multiple clauses available to work with HQL.

The FROM clause

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:

Code

Query query = session.createQuery("FROM Category");
List<Category> list = query.list();
System.out.println("Category size: " + list.size());

Output

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.

Note

In HQL statements, the class name, used instead of the table, and fields, used instead of the columns, are case-sensitive; we can use the other part of the query in any case. For example, we can use fRoM or From instead of FROM.

We can use multiple classes in the same query, which results in a Cartesian product or cross join.

Execute the following code:

Code

Query query = session.createQuery("FroM Category, Product");
List list = query.list();
System.out.println("Result size: " + list.size());

Output

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 SELECT clause

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:

Code

Query query = session.createQuery("SELECT id, name from Category");
List list = query.list();
System.out.println("Result size: " + list.size());

Output

Hibernate: select category0_.id as col_0_0_, category0_.name as col_1_0_ from category category0_
Result size: 2

Here, hibernate creates a SQL query with only two fields, which is given in HQL.

How it works…

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.

There's more…

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:

The WHERE clause

Here is an example:

String hql = "FROM Category c WHERE c.id=1";

The ORDER BY clause

Here is an example:

String hql = "FROM Category c ORDER BY c.id DESC";

The GROUP BY clause

Here is an example:

String hql = "SELECT COUNT(p.id), p.name FROM Product p GROUP BY p.category";

Apart from this, we can use subquery, joins, named queries, and expressions such as mathematical, logical, comparison, Update, and Delete.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.85.221