<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div style="color: #20124d;">Hibernate created a new language named Hibernate Query Language (HQL), the syntax is quite similar to database SQL language. The main difference between is <b>HQL uses class name instead of table name, and property names instead of column name</b>.</div>
<div id="ads-id" align="center"></div>
<div style="color: #20124d;">Hibernate uses the following ways to retrieve objects from the database:</div>
<ul style="color: #20124d;">
<li>Hibernate Query Language (<b>HQL</b>)</li>
<li>Query By Criteria (<b>QBC</b>) and Query BY Example (<b>QBE</b>) using <b>Criteria</b> API</li>
<li>Native <b>SQL</b> queries</li>
</ul>
<p>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 <i style="color: #20124d;">aggregate functions</i> (for example: sum(), max()) and clauses such as <i style="color: #20124d;">group by</i> and <i style="color: #20124d;">order by</i> clause.</p>
<h2><b style="color: blue;">Why WE USE HQL?</b></h2>
</div>
<div style="color: #20124d;">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.</div>
<ul style="color: #20124d;">
<li>HQL allows representing SQL queries in object-oriented terms—by using objects and properties of objects.</li>
<li>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 &#8220;resultset&#8221; retrieved from database queried.</li>
<li>HQL fully supports <b>polymorphic queries</b>. 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.</li>
<li>HQL is easy to learn and implement, as its syntax and features are very similar to SQL.</li>
<li>HQL contains many advance features such as pagination, fetch join with dynamic profiling, and so forth, as compared to SQL.</li>
<li>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.</li>
</ul>
<h3 style="color: blue;">HQL Syntax>;>;</h3>
<div style="color: #20124d;">As described earlier, most of HQL&#8217;s syntax and features are very similar to SQL. An HQL query may consist of following elements:</div>
<ul style="color: #20124d;">
<li>Clauses</li>
<li>Aggregate functions</li>
<li>Subqueries</li>
</ul>
<h2 style="color: #20124d;"><b>Clauses in the HQL are:</b></h2>
<ul style="color: #20124d;">
<li><a href="https://dineshonjava.com/hql-from-clause-example/"><b>from</b></a></li>
<li><a href="https://dineshonjava.com/hibernate-select-clause/"><b>select</b></a></li>
<li><a href="https://dineshonjava.com/hql-where-clause-example/"><b>where</b></a></li>
<li><a href="https://dineshonjava.com/hql-order-by-example/"><b>order by</b></a></li>
<li><b></b><b><a href="https://dineshonjava.com/hql-group-by-clause-example/">group by</a> </b></li>
</ul>
</div>
<h2 style="color: #20124d;"><b>Aggregate functions are:</b></h2>
<ul style="color: #20124d;">
<li><a href="https://dineshonjava.com/hibernate-supports-multiple-aggregate/"><b>avg(&#8230;)</b></a>, <a href="https://dineshonjava.com/hibernate-supports-multiple-aggregate/"><b>sum(&#8230;)</b></a>, <a href="https://dineshonjava.com/hibernate-supports-multiple-aggregate/"><b>min(&#8230;)</b></a>, <b> <a href="https://dineshonjava.com/hibernate-supports-multiple-aggregate/">max(&#8230;)</a></b></li>
<li><a href="https://dineshonjava.com/hibernate-count-query/"><b>count(*)</b></a></li>
<li>count(&#8230;), count(distinct &#8230;), count(all&#8230;)</li>
</ul>
<h2 style="color: #20124d;"><b>Subqueries</b></h2>
<div style="color: #20124d;">Subqueries are nothing but its a query within another query. Hibernate supports Subqueries if the underlying database supports it.</div>
<p> ;</p>
<div style="color: #20124d;"><b>Table A:</b> HQL Clauses with their description, syntax, and examples.</div>
<table style="color: #20124d;" border="2" cellspacing="2" cellpadding="2">
<tbody>
<tr valign="top" bgcolor="#ffff99">
<td><b>Clause</b></td>
<td><b>Description</b></td>
<td><b>Syntax</b></td>
<td><b>Example</b></td>
</tr>
<tr valign="top">
<td><b>from</b></td>
<td>The simplest form of an HQL query. Specifies the object whose instances are to be returned as the query result. Commonly used with the <b>select</b> clause.</td>
<td><b>from</b> <i>object</i> [<b>as</b> <i>object_alias</i>]* object_alias simply means another name given to refer to an object for convenience.</td>
<td>from UserDetails as user</p>
<p>Will return all instances of object <i>UserDetails.</i></td>
</tr>
<tr valign="top">
<td><b>select</b></td>
<td>Specifies objects and properties to be returned in the query result set. Used in conjunction with the <b>from</b> clause.</td>
<td><b>select</b> [<i>object.</i>]<i>property</i></td>
<td>select user.userName from UserDetails as user</p>
<p>Will return all values of userName in all instances of <i>UserDetails.</i></td>
</tr>
<tr valign="top">
<td><b>where</b></td>
<td>Specifies the conditions that should be satisfied by the instances returned as the query result. Used with <b>select</b> and/or <b>from</b> clause.</td>
<td><b>where</b> <i>condition</i></p>
<p>Here, condition is a combination of logical, relational operators i.e. =, >;, AND, NOT etc.</td>
<td>from UserDetails as user where user.userId >; 2<br />
Will return all instances of user in UserDetails whose correspondinguser.userId values are greater than 2.</td>
</tr>
<tr valign="top">
<td><b>order by</b></td>
<td>Specifies the order (<b>asc</b>ending/<b>desc</b>ending) in which the properties of objects returned as query results should be listed. Used with the <b>select</b> and <b>from</b> clauses.</td>
<td><b>order by</b> <i>object0.property0</i> [<b>asc</b>|<b>desc</b>][, <i>object1.property0</i>]&#8230;</p>
<p>By default, order is ascending unless specified otherwise.</td>
<td>from UserDetails as user order by userId asc</p>
<p>Will return a list of all instances of user in ascending order of corresponding userId values.</td>
</tr>
<tr valign="top">
<td><b>group by</b></td>
<td>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 <b>select</b> and/or <b>from</b> clause.</td>
<td><b>group by</b> <i>object0.property0</i>[, <i>object1.property0</i>]&#8230;</td>
<td>select userId from UserDetails as user group by user.userId</p>
<p>Will return list of all userId instances from user grouped by corresponding values of user.</td>
</tr>
</tbody>
</table>
</div>
<p><b>In the <a href="https://dineshonjava.com/p/select-and-pagination-in-hql.html">Next Chapter</a> we will discuss about the <a href="https://dineshonjava.com/p/select-and-pagination-in-hql.html">How work Pagination in Hibernate Query Language(HQL)</a>.</b><br />
<b><br />
</b> <b> <;<;<a style="font-family: Times, 'Times New Roman', serif;" href="https://dineshonjava.com/p/understanding-state-changes-of-object.html">Previous Chapter 25</a><;<; >;>;N<a href="https://dineshonjava.com/p/select-and-pagination-in-hql.html">ext Chapter 27</a>>;>;</b></p>
<p> ;</p>
</div>