Hibernate HQL
Hibernate HQL
HQL
Hibernate Query Language (HQL) is same as SQL (Structured Query Language) but it
doesn't depends on the table of the database.
Instead of table name, we use class name in HQL. So it is database independent query
language.
Hibernate converts HQL queries into SQL queries, which are then used to perform
database actions.
HQL has many benefits. Some benefits are – they are database independent, polymorphic
queries supported, and easy to learn for Java programmers
The Query interface provides object-oriented methods and capabilities for representing
and manipulating HQL queries
HQL Clauses
HQL is case-insensitive except for java class and variable names. So SeLeCT is the same as
sELEct is the same as SELECT, but com.brainpower.model.Employee is not same as
com.brainpower.model.EMPLOYEE
Keywords like SELECT, FROM, and WHERE, etc., are not case sensitive, but properties like
table and column names are case sensitive in HQL
1. HQL From:
HQL From is same as select clause in SQL, from Employee is same as select * from
Employee. We can also create alias such as from Employee emp or from Employee as
emp.
2. HQL Join :
HQL supports inner join, left outer join, right outer join and full join.
For example, select e.name, a.city from Employee e INNER JOIN e.address a.
In this query, Employee class should have a variable named address.
3. Aggregate Functions:
HQL supports commonly used aggregate functions such as count(*), count(distinct x),
min(), max(), avg() and sum().
4. Expressions:
HQL supports arithmetic expressions (+, -, *, /), binary comparison operators (=, >=,
<=, <>, !=, like), logical operations (and, or, not) etc.
1. FROM Clause
FROM clause if you want to load a complete persistent objects into memory
Syntax:
Syntax:
AS keyword is optional and you can also specify the alias directly after the
class name
Syntax:
3. SELECT Clause
The SELECT clause is used when only a few attributes of an object are
required rather than the entire object.
Syntax:
4. WHERE Clause
Filtering records is done with the WHERE clause. It’s used to retrieve only
the records that meet a set of criteria.
Syntax:
To sort your HQL query's results, you will need to use the ORDER BY clause. You
can order the results by any property on the objects in the result set either ascending
(ASC) or descending (DESC)
Syntax:
String hql = "FROM Employee E WHERE E.id > 10 ORDER BY E.salary DESC";
6. GROUP BY Clause
This clause lets Hibernate pull information from the database and group it based on a
value of an attribute and, typically, use the result to include an aggregate value
Syntax:
"GROUP BY E.firstName";
7. NAMED Parameters
Hibernate supports named parameters in its HQL queries. This makes writing HQL
queries that accept input from the user easy and you do not have to defend against SQL
injection attacks.
Syntax:
query.setParameter("employee_id",10);
8. UPDATE Clause
The UPDATE clause can be used to update one or more properties of an one or
more objects
Syntax:
"WHERE id = :employee_id";
query.setParameter("salary", 1000);
query.setParameter("employee_id", 10);
9. DELETE Clause
Syntax:
"WHERE id = :employee_id";
query.setParameter("employee_id", 10);
Syntax:
Query setMaxResults(int
Instructs Hibernate to get a specific number of items
max)
Syntax:
Example 5: sum