Hibernate Query


Hibernate Query

In contrast to SQL, Hibernate Query Language (HQL) operates on persistent objects and their properties. Hibernate translates HQL queries into standard SQL queries, which in turn conduct actions on the database due to their translation.

Whenever feasible, I propose utilizing HQL to prevent database portability issues and benefit from Hibernate's SQL generation and caching tactics.

Table and column names are case-sensitive in HQL but not SELECT, FROM, and WHERE.FROM Clause

FROM Clause

If you wish to load an entire persistent object into memory, you will use the FROM clause.

AS Clause

It is possible to provide aliases to classes in your HQL queries using the AS clause, especially if you have very long queries.

SELECT Clause

The SELECT statement provides more powerful control over the result set than is provided by the statement. Use the SELECT clause if you only want a few properties of an object rather than the entire object.

WHERE Clause

The WHERE clause is used when you wish to filter down the exact objects returned from storage.

ORDER BY Clause

To sort the results of your HQL query, you'll need to utilize the ORDER BY statement. Ascending (ASC) or descending (DSC) results can be ordered by any property on the objects in the result set (DESC).

GROUP BY Clause

Hibernate can retrieve information from the database, group it by an attribute value, and provide an aggregate value by specifying this clause.

Using Named Parameters

With Hibernate, named arguments can be used in HQL queries. In addition, you do not have to protect against SQL injection threats when writing HQL queries that accept input from the user.

UPDATE Clause

As of Hibernate 3, HQL now supports bulk updates, and delete operations function differently than in Hibernate 2. Now you may execute UPDATE or DELETE statements using executeUpdate(), which was added to the Query interface.
To change one or more properties of an object, the UPDATE clause can be used.

DELETE Clause

To delete an object, use the DELETE clause.

INSERT Clause

Records can only be inserted from one object into another object when using the INSERT INTO clause with HQL.