Nearby lessons

14 of 19

Hibernate - HQL (Hibernate Query Language)

📌 What You Will Learn
  • Understand Hibernate - HQL (Hibernate Query Language)
  • See working code examples
  • Learn from common mistakes and Q&A

Learn Hibernate - HQL (Hibernate Query Language) step by step — simple explanations, complete programs with their output, common beginner mistakes, and exam-style MCQs.

HQL (Hibernate Query Language) — Test.java

  • <mapping resource="Employee.hbm.xml"/>
  • <mapping resource="Employee2.hbm.xml"/>
  • <mapping class="com.durgasoft.pojo.Employee1"/>
  • <mapping class="com.durgasoft.pojo.Employee2"/>
  • </session-factory>
  • </hibernate-configuration>
Example01
Loading editor…

Building Blocks for HQL queries

To prepare HQL queries what are the various elements we have to provide

  • Clauses
  • Aggregate Functions
  • Generic Expressions
  • Parameters
  • Subqueries

Clauses

These are building blocks to HQL queries, which are able to specify POJO classes property names, conditional expressions,.....

EX:

from select where order by group by having ---- ----

from

It can be used to specify POJO class names and their alias names in HQL queries.

Syntax:

From POJO_Class_Name [[AS] var_Name]

EX:

From Employee FROM Employee from Employee e From Employee AS e

select

This clause can be used to specify POJO class properties names inorder to retrive individual column values. If we provide select clause with individual POJO class properties in HQL query then results are generated in the form of Object[].

Syntax:

[select prop_Names] from POJO_Class_Name [[AS] var_Name]

Example05
Loading editor…

Where clause

In HQL, where clause can be used to provide a particular conditional expression inorder to retrive the results as per the condition.

Syntax:

[select prop_Names] from POJO_Class_Name [[AS] var_Name][where condition]

Example06
Loading editor…

Order by

It can be used to retrive all results either in ascending order or in descending order w.r.t a particular column. in HQL, bydefault, all results are generated in Ascending order only.

Syntax:

[select prop_Names] from POJO_Class_Name [[AS] var_Name][where condition][order by prop_Name asc/desc]

Example07
Loading editor…

group by

This clause can be used to specify groups over the retrived results w.r.t a particular column. Note: In HQL queries, we are able to implement group by clause with the combination of aggregate functions only.

Syntax:

[select prop_Names] from POJO_Class_Name [[AS] var_Name][where condition][order by prop_Name asc/desc] [group by column_Name]

Example08
Loading editor…

group by

The main intention of having clause is to implement a conditional expression while including elements in groups as per group by clause.

Syntax:

[select prop_Names] from POJO_Class_Name [[AS] var_Name][where condition][order by prop_Name asc/desc] [group by column_Name][having Condition]

Example09
Loading editor…

Aggregate Functions

The main intention of Aggregate functions is to provide small arithmetic calculations over the results.

Example10
Loading editor…

Aggregate Functions

Example11
Loading editor…

Aggregate Functions

  • sum: It able to perform addition operation over the results which are generated from HQl query.
Example12
Loading editor…

Aggregate Functions

  • min: It able to get min value over all the results which are generated from HQl query.
Example13
Loading editor…

Aggregate Functions

  • max: It able to get max value over all the results which are generated from HQl query.
Example14
Loading editor…

Aggregate Functions

  • avg: It able to generate average value over all the generated results from HQL query.
Example15
Loading editor…

Generic Expressions

The main intention of Generic Expressions is to provide expressions in HQL queries inorder to perform simple Arithmetic calculations, comparisions,......

To provide generic expressions in HQL queries we have to use the following elements.

a)Arithmetic Operators: +, -, *, /, %,......

Example16
Loading editor…

Generic Expressions

b)Comparision Operators: ==, !=, <, >, <=, >= ,....

c)Logical Operators: && or AND, || or OR ,....

Example17
Loading editor…

d)Scalar Functions

lower(): To get results in lower case letters. upper(): To get results in Upper case letters.

Example18
Loading editor…
📝 Key Takeaways
  • Key ideas of Hibernate - HQL (Hibernate Query Language) explained simply
  • Ready-to-use code examples
  • Exam-style questions at the end

Finished this lesson?

Mark it as complete to track your progress and see technology completion stats.