Topic 8 – Hibernate Query Language (HQL)

image 2

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:

  1. Retrieving all objects of a certain type from the database (FROM clause).
  2. Retrieving a single object based on its ID (WHERE clause and setParameter() method).
  3. Updating an object in the database (update() method).
  4. Deleting an object from the database (delete() method).
  5. Filtering objects based on a certain property (WHERE clause and LIKE operator).
  6. Joining tables and filtering based on a related object’s property (p.address.city).
  7. Filtering objects based on a range of values (BETWEEN operator).
  8. Retrieving a count of objects (SELECT COUNT(*)).
  9. Retrieving an aggregate value (MAX(), AVG()).
  10. Retrieving specific properties of objects as a DTO (SELECT new syntax).

Github Project link – https://github.com/kuldeep101990/HQLHibernateQuery

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *