
Play Store Application link β Hibernate – in 10 steps – Apps on Google Play
Definition –
Hibernate Query Language (HQL) is used to perform database operations on objects. HQL allows you to perform SQL-like queries on your objects, and provides a way to navigate through object graphs using object-oriented syntax.
A- Performing database operations using HQL: – To perform database operations using HQL, you must first create a Hibernate Session object, which is used to manage database operations. Once you have a Session object, you can use HQL to query your objects and perform operations on them.
B- Understanding HQL syntax: – HQL is similar to SQL in syntax, but instead of working with tables and columns, it works with objects and their properties. HQL queries are written in a high-level, object-oriented syntax that is easy to read and understand.
Here’s an example of an HQL query that retrieves all Person objects from the database:
String hql = "FROM Person";
Query query = session.createQuery(hql);
List<Person> persons = query.list();
C- Using HQL to query objects: HQL can be used to query objects based on their properties, relationships, and other criteria. Here are a few examples:
- Querying objects based on a property:
String hql = "FROM Person p WHERE p.name = 'Kuldeep kaushik'";
Query query = session.createQuery(hql);
List<Person> persons = query.list();
In this example, the HQL query retrieves all Person objects whose name is “Kuldeep kaushik”.
- Querying objects based on a relationship:
String hql = "FROM Person p WHERE p.address.city = 'noida'";
Query query = session.createQuery(hql);
List<Person> persons = query.list();
In this example, the HQL query retrieves all Person objects whose address is in noida.
- Querying objects based on a date range:
String hql = "FROM Person p WHERE p.dob BETWEEN :startDate AND :endDate";
Query query = session.createQuery(hql);
query.setParameter("startDate", new Date());
query.setParameter("endDate", new Date());
List<Person> persons = query.list();
In this example, the HQL query retrieves all Person objects whose date of birth is between the current date and the current date.
10 Features of Query Object in Hibernate-
This example demonstrates the following features of the Query object:
- Retrieving all objects of a certain type from the database (
FROM
clause). - Retrieving a single object based on its ID (
WHERE
clause andsetParameter()
method). - Updating an object in the database (
update()
method). - Deleting an object from the database (
delete()
method). - Filtering objects based on a certain property (
WHERE
clause andLIKE
operator). - Joining tables and filtering based on a related object’s property (
p.address.city
). - Filtering objects based on a range of values (
BETWEEN
operator). - Retrieving a count of objects (
SELECT COUNT(*)
). - Retrieving an aggregate value (
MAX()
,AVG()
). - Retrieving specific properties of objects as a DTO (
SELECT new
syntax).
Github Project link – https://github.com/kuldeep101990/HQLHibernateQuery
[…] Step 8 β Hibernate Query Language (HQL) […]