Hibernate HQL Operation – Deleting Records in Java
Table of Contents
- Introduction
- Understanding HQL Delete Operation
- Setting Up the Project Environment
- Implementation of HQL “Delete” Operation
- Comparison Table
- Conclusion
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
1 |
DELETE FROM EntityName WHERE condition |
For example:
1 |
DELETE FROM Users WHERE name = 'John Doe' |
Setting Up the Project Environment
1. Hibernate Configuration File
1 2 3 4 5 6 |
com.mysql.cj.jdbc.Driver jdbc:mysql://localhost:3306/test_db root password org.hibernate.dialect.MySQL8Dialect true |
2. Entity Class: Users.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
package org.studyeasy.entity; import javax.persistence.Entity; import javax.persistence.Id; @Entity public class Users { @Id private int id; private String name; private String email; public Users() {} public Users(int id, String name, String email) { this.id = id; this.name = name; this.email = email; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } |
3. HQL Implementation: App.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
package org.studyeasy; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.query.Query; public class App { public static void main(String[] args) { SessionFactory factory = new Configuration() .configure("hibernate.cfg.xml") .addAnnotatedClass(org.studyeasy.entity.Users.class) .buildSessionFactory(); Session session = factory.openSession(); try { Transaction transaction = session.beginTransaction(); // HQL Delete Query String hql = "DELETE FROM Users WHERE name = 'Chand'"; Query query = session.createQuery(hql); int result = query.executeUpdate(); System.out.println("Number of records deleted: " + result); transaction.commit(); } catch (Exception e) { e.printStackTrace(); } finally { session.close(); factory.close(); } } } |
Implementation of HQL “Delete” Operation
Code Walkthrough
- SessionFactory: Initializes Hibernate configuration.
- Transaction: Starts the database operation.
- Delete Query:
12String hql = "DELETE FROM Users WHERE name = 'Chand'";Query query = session.createQuery(hql); - Execution and Output:
12int result = query.executeUpdate();System.out.println("Number of records deleted: " + result);
Program Output
1 |
Number of records deleted: 1 |
Database Table (After Deletion)
ID | Name | |
---|---|---|
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.