S02L04 – Hibernate HQL operation – Delete


Hibernate HQL Operation – Deleting Records in Java

Table of Contents

Introduction

CRUD stands for:

  • Create – Add new records.
  • Read – Retrieve existing data.
  • Update – Modify data.
  • Delete – Remove records.

This article focuses on the “Delete Operation” in Hibernate HQL.

CRUD Table

CRUD Operation Purpose Example
Create Add new records Insert user data
Read Fetch existing records Retrieve user info
Update Modify records Update email
Delete Remove records Delete account

Understanding HQL Delete Operation

What is HQL?

Hibernate Query Language (HQL) is a query language that enables database interactions using entity objects.

HQL Delete Syntax

For example:

Setting Up the Project Environment

1. Hibernate Configuration File

2. Entity Class: Users.java

3. HQL Implementation: App.java

Implementation of HQL “Delete” Operation

Code Walkthrough

  • SessionFactory: Initializes Hibernate configuration.
  • Transaction: Starts the database operation.
  • Delete Query:
  • Execution and Output:

Program Output

Database Table (After Deletion)

ID Name Email
2 Jane Smith [email protected]

Comparison Table

Feature HQL Delete SQL Delete
Target Entity Objects Table Rows
Database Independence Yes No
Syntax DELETE FROM EntityName DELETE FROM TableName

Conclusion

We explored the **Hibernate HQL Delete Operation** in Java. Using HQL, developers can delete records efficiently without writing raw SQL queries. Key concepts like Session, Transaction, and HQL syntax were covered in detail.

Key Takeaways:

  • HQL simplifies database operations.
  • The DELETE statement removes records based on conditions.
  • Use Transaction to ensure consistency during database operations.