Hibernate Query Language and the Query Object

<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<div style&equals;"color&colon; &num;20124d&semi;">Hibernate created a new language named Hibernate Query Language &lpar;HQL&rpar;&comma; the syntax is quite similar to database SQL language&period; The main difference between is <b>HQL uses class name instead of table name&comma; and property names instead of column name<&sol;b>&period;<&sol;div>&NewLine;<div id&equals;"ads-id" align&equals;"center"><&sol;div>&NewLine;<div style&equals;"color&colon; &num;20124d&semi;">Hibernate uses the following ways to retrieve objects from the database&colon;<&sol;div>&NewLine;<ul style&equals;"color&colon; &num;20124d&semi;">&NewLine;<li>Hibernate Query Language &lpar;<b>HQL<&sol;b>&rpar;<&sol;li>&NewLine;<li>Query By Criteria &lpar;<b>QBC<&sol;b>&rpar; and Query BY Example &lpar;<b>QBE<&sol;b>&rpar; using <b>Criteria<&sol;b> API<&sol;li>&NewLine;<li>Native <b>SQL<&sol;b> queries<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>The most preferred way is using the Hibernate Query Language &lpar;HQL&rpar;&comma; which is an easy-to-learn and powerful query language&comma; designed as a minimal object-oriented extension to SQL&period; HQL has syntax and keywords&sol;clauses very similar to SQL&period; It also supports many other SQL-like features&comma; such as <i style&equals;"color&colon; &num;20124d&semi;">aggregate functions<&sol;i> &lpar;for example&colon; sum&lpar;&rpar;&comma; max&lpar;&rpar;&rpar; and clauses such as <i style&equals;"color&colon; &num;20124d&semi;">group by<&sol;i> and <i style&equals;"color&colon; &num;20124d&semi;">order by<&sol;i> clause&period;<&sol;p>&NewLine;<h2><b style&equals;"color&colon; blue&semi;">Why WE USE HQL&quest;<&sol;b><&sol;h2>&NewLine;<&sol;div>&NewLine;<div style&equals;"color&colon; &num;20124d&semi;">Although it is possible to use native SQL queries directly with a Hibernate-based persistence layer&comma; it is more efficient to use HQL instead&period; The reasons of choosing HQL over the other two methods are given below&period;<&sol;div>&NewLine;<ul style&equals;"color&colon; &num;20124d&semi;">&NewLine;<li>HQL allows representing SQL queries in object-oriented terms—by using objects and properties of objects&period;<&sol;li>&NewLine;<li>Instead of returning plain data&comma; HQL queries return the query result&lpar;s&rpar; in the form of object&lpar;s&rpar;&sol;tuples of object&lpar;s&rpar; that are ready to be accessed&comma; operated upon&comma; and manipulated programmatically&period; This approach does away with the routine task of creating and populating objects from scratch with the &&num;8220&semi;resultset&&num;8221&semi; retrieved from database queried&period;<&sol;li>&NewLine;<li>HQL fully supports <b>polymorphic queries<&sol;b>&period; That is&comma; along with the object to be returned as a query result&comma; all child objects &lpar;objects of subclasses&rpar; of the given object shall be returned&period;<&sol;li>&NewLine;<li>HQL is easy to learn and implement&comma; as its syntax and features are very similar to SQL&period;<&sol;li>&NewLine;<li>HQL contains many advance features such as pagination&comma; fetch join with dynamic profiling&comma; and so forth&comma; as compared to SQL&period;<&sol;li>&NewLine;<li>HQL facilitates writing database-type independent queries that are converted to the native SQL dialect of the underlying database at runtime&period; This approach helps tap the extra features the native SQL query provides&comma; without using a non-standard native SQL query&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h3 style&equals;"color&colon; blue&semi;">HQL Syntax&gt&semi;&gt&semi;<&sol;h3>&NewLine;<div style&equals;"color&colon; &num;20124d&semi;">As described earlier&comma; most of HQL&&num;8217&semi;s syntax and features are very similar to SQL&period; An HQL query may consist of following elements&colon;<&sol;div>&NewLine;<ul style&equals;"color&colon; &num;20124d&semi;">&NewLine;<li>Clauses<&sol;li>&NewLine;<li>Aggregate functions<&sol;li>&NewLine;<li>Subqueries<&sol;li>&NewLine;<&sol;ul>&NewLine;<h2 style&equals;"color&colon; &num;20124d&semi;"><b>Clauses in the HQL are&colon;<&sol;b><&sol;h2>&NewLine;<ul style&equals;"color&colon; &num;20124d&semi;">&NewLine;<li><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hql-from-clause-example&sol;"><b>from<&sol;b><&sol;a><&sol;li>&NewLine;<li><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hibernate-select-clause&sol;"><b>select<&sol;b><&sol;a><&sol;li>&NewLine;<li><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hql-where-clause-example&sol;"><b>where<&sol;b><&sol;a><&sol;li>&NewLine;<li><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hql-order-by-example&sol;"><b>order by<&sol;b><&sol;a><&sol;li>&NewLine;<li><b><&sol;b><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hql-group-by-clause-example&sol;">group by<&sol;a> <&sol;b><&sol;li>&NewLine;<&sol;ul>&NewLine;<&sol;div>&NewLine;<h2 style&equals;"color&colon; &num;20124d&semi;"><b>Aggregate functions are&colon;<&sol;b><&sol;h2>&NewLine;<ul style&equals;"color&colon; &num;20124d&semi;">&NewLine;<li><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hibernate-supports-multiple-aggregate&sol;"><b>avg&lpar;&&num;8230&semi;&rpar;<&sol;b><&sol;a>&comma; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hibernate-supports-multiple-aggregate&sol;"><b>sum&lpar;&&num;8230&semi;&rpar;<&sol;b><&sol;a>&comma; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hibernate-supports-multiple-aggregate&sol;"><b>min&lpar;&&num;8230&semi;&rpar;<&sol;b><&sol;a>&comma; <b> <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hibernate-supports-multiple-aggregate&sol;">max&lpar;&&num;8230&semi;&rpar;<&sol;a><&sol;b><&sol;li>&NewLine;<li><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hibernate-count-query&sol;"><b>count&lpar;&ast;&rpar;<&sol;b><&sol;a><&sol;li>&NewLine;<li>count&lpar;&&num;8230&semi;&rpar;&comma; count&lpar;distinct &&num;8230&semi;&rpar;&comma; count&lpar;all&&num;8230&semi;&rpar;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h2 style&equals;"color&colon; &num;20124d&semi;"><b>Subqueries<&sol;b><&sol;h2>&NewLine;<div style&equals;"color&colon; &num;20124d&semi;">Subqueries are nothing but its a query within another query&period; Hibernate supports Subqueries if the underlying database supports it&period;<&sol;div>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<div style&equals;"color&colon; &num;20124d&semi;"><b>Table A&colon;<&sol;b> HQL Clauses with their description&comma; syntax&comma; and examples&period;<&sol;div>&NewLine;<table style&equals;"color&colon; &num;20124d&semi;" border&equals;"2" cellspacing&equals;"2" cellpadding&equals;"2">&NewLine;<tbody>&NewLine;<tr valign&equals;"top" bgcolor&equals;"&num;ffff99">&NewLine;<td><b>Clause<&sol;b><&sol;td>&NewLine;<td><b>Description<&sol;b><&sol;td>&NewLine;<td><b>Syntax<&sol;b><&sol;td>&NewLine;<td><b>Example<&sol;b><&sol;td>&NewLine;<&sol;tr>&NewLine;<tr valign&equals;"top">&NewLine;<td><b>from<&sol;b><&sol;td>&NewLine;<td>The simplest form of an HQL query&period; Specifies the object whose instances are to be returned as the query result&period; Commonly used with the <b>select<&sol;b> clause&period;<&sol;td>&NewLine;<td><b>from<&sol;b> <i>object<&sol;i> &lbrack;<b>as<&sol;b> <i>object&lowbar;alias<&sol;i>&rsqb;&ast; object&lowbar;alias simply means another name given to refer to an object for convenience&period;<&sol;td>&NewLine;<td>from UserDetails as user<&sol;p>&NewLine;<p>Will return all instances of object <i>UserDetails&period;<&sol;i><&sol;td>&NewLine;<&sol;tr>&NewLine;<tr valign&equals;"top">&NewLine;<td><b>select<&sol;b><&sol;td>&NewLine;<td>Specifies objects and properties to be returned in the query result set&period; Used in conjunction with the <b>from<&sol;b> clause&period;<&sol;td>&NewLine;<td><b>select<&sol;b> &lbrack;<i>object&period;<&sol;i>&rsqb;<i>property<&sol;i><&sol;td>&NewLine;<td>select user&period;userName from UserDetails as user<&sol;p>&NewLine;<p>Will return all values of userName in all instances of <i>UserDetails&period;<&sol;i><&sol;td>&NewLine;<&sol;tr>&NewLine;<tr valign&equals;"top">&NewLine;<td><b>where<&sol;b><&sol;td>&NewLine;<td>Specifies the conditions that should be satisfied by the instances returned as the query result&period; Used with <b>select<&sol;b> and&sol;or <b>from<&sol;b> clause&period;<&sol;td>&NewLine;<td><b>where<&sol;b> <i>condition<&sol;i><&sol;p>&NewLine;<p>Here&comma; condition is a combination of logical&comma; relational operators i&period;e&period; &equals;&comma; &gt&semi;&comma; AND&comma; NOT etc&period;<&sol;td>&NewLine;<td>from UserDetails as user where user&period;userId &gt&semi; 2<br &sol;>&NewLine;Will return all instances of user in UserDetails whose correspondinguser&period;userId values are greater than 2&period;<&sol;td>&NewLine;<&sol;tr>&NewLine;<tr valign&equals;"top">&NewLine;<td><b>order by<&sol;b><&sol;td>&NewLine;<td>Specifies the order &lpar;<b>asc<&sol;b>ending&sol;<b>desc<&sol;b>ending&rpar; in which the properties of objects returned as query results should be listed&period; Used with the <b>select<&sol;b> and <b>from<&sol;b> clauses&period;<&sol;td>&NewLine;<td><b>order by<&sol;b> <i>object0&period;property0<&sol;i> &lbrack;<b>asc<&sol;b>&vert;<b>desc<&sol;b>&rsqb;&lbrack;&comma; <i>object1&period;property0<&sol;i>&rsqb;&&num;8230&semi;<&sol;p>&NewLine;<p>By default&comma; order is ascending unless specified otherwise&period;<&sol;td>&NewLine;<td>from UserDetails  as user order by userId asc<&sol;p>&NewLine;<p>Will return a list of all instances of user in ascending order of corresponding userId values&period;<&sol;td>&NewLine;<&sol;tr>&NewLine;<tr valign&equals;"top">&NewLine;<td><b>group by<&sol;b><&sol;td>&NewLine;<td>Specifies the grouping criteria using objects properties&comma; by which the list of objects returned as a query result should be grouped together&period; Used with the <b>select<&sol;b> and&sol;or <b>from<&sol;b> clause&period;<&sol;td>&NewLine;<td><b>group by<&sol;b> <i>object0&period;property0<&sol;i>&lbrack;&comma; <i>object1&period;property0<&sol;i>&rsqb;&&num;8230&semi;<&sol;td>&NewLine;<td>select userId from UserDetails as user group by user&period;userId<&sol;p>&NewLine;<p>Will return list of all userId instances from user grouped by corresponding values of user&period;<&sol;td>&NewLine;<&sol;tr>&NewLine;<&sol;tbody>&NewLine;<&sol;table>&NewLine;<&sol;div>&NewLine;<p><b>In the <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;p&sol;select-and-pagination-in-hql&period;html">Next Chapter<&sol;a> we will discuss about the <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;p&sol;select-and-pagination-in-hql&period;html">How work Pagination in Hibernate Query Language&lpar;HQL&rpar;<&sol;a>&period;<&sol;b><br &sol;>&NewLine;<b><br &sol;>&NewLine;<&sol;b> <b>                         &lt&semi;&lt&semi;<a style&equals;"font-family&colon; Times&comma; 'Times New Roman'&comma; serif&semi;" href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;p&sol;understanding-state-changes-of-object&period;html">Previous Chapter 25<&sol;a>&lt&semi;&lt&semi;    &gt&semi;&gt&semi;N<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;p&sol;select-and-pagination-in-hql&period;html">ext Chapter 27<&sol;a>&gt&semi;&gt&semi;<&sol;b><&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<&sol;div>&NewLine;