Nearby lessons
14 of 19Hibernate - HQL (Hibernate Query Language)
- 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>
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]
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]
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]
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]
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]
Aggregate Functions
The main intention of Aggregate functions is to provide small arithmetic calculations over the results.
Aggregate Functions
Aggregate Functions
- sum: It able to perform addition operation over the results which are generated from HQl query.
Aggregate Functions
- min: It able to get min value over all the results which are generated from HQl query.
Aggregate Functions
- max: It able to get max value over all the results which are generated from HQl query.
Aggregate Functions
- avg: It able to generate average value over all the generated results from HQL query.
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: +, -, *, /, %,......
Generic Expressions
b)Comparision Operators: ==, !=, <, >, <=, >= ,....
c)Logical Operators: && or AND, || or OR ,....
d)Scalar Functions
lower(): To get results in lower case letters. upper(): To get results in Upper case letters.
- Key ideas of Hibernate - HQL (Hibernate Query Language) explained simply
- Ready-to-use code examples
- Exam-style questions at the end