Hibernate CRUD Operation: Delete
Table of Contents
- Introduction
- Understanding Hibernate Delete Operation
- What is Hibernate?
- What is a CRUD Operation?
- Focus on Delete Operation
- Implementing Delete in Hibernate
- Code Explanation: Users.java
- Code Explanation: App.java
- Comparison Table: CRUD Operations
- Output and Explanation
- Conclusion
Introduction
Hibernate is an ORM (Object Relational Mapping) framework used in Java applications to interact with relational databases. It simplifies database operations by allowing developers to work with objects rather than SQL queries directly.
This article focuses on the Hibernate Delete Operation, a key part of CRUD operations.
CRUD Operation | Description |
---|---|
Create | Adds new records to the database. |
Read | Retrieves existing data. |
Update | Modifies existing data. |
Delete | Removes specific records. |
Understanding Hibernate Delete Operation
What is Hibernate?
Hibernate is a popular Java-based framework that allows developers to map Java classes to database tables.
What is a CRUD Operation?
CRUD stands for Create, Read, Update, and Delete—basic operations performed on a database.
Focus on Delete Operation
The delete operation uses the session.delete() method to remove a specific record from the database.
Implementing Delete in Hibernate
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 |
package org.studyeasy.entity; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; @Entity(name="users") @Table(name="users") public class Users { @Id @Column(name="user_id") int userId; @Column(name="username") String username; @Column(name="password") String password; @Column(name="first_name") String firstname; @Column(name="last_name") String lastname; public Users() { super(); } public Users(String username, String password, String firstname, String lastname) { this.username = username; this.password = password; this.firstname = firstname; this.lastname = lastname; } // Getters and Setters... } |
Main Application: 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 37 38 |
package org.studyeasy; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.studyeasy.entity.Users; public class App { public static void main(String[] args) { SessionFactory factory = new Configuration() .configure("hibernate.cfg.xml") .addAnnotatedClass(Users.class) .buildSessionFactory(); Session session = factory.getCurrentSession(); try { // Start transaction session.beginTransaction(); // Retrieve user with ID 3 Users user = session.get(Users.class, 3); // Delete the user session.delete(user); // Commit transaction session.getTransaction().commit(); System.out.println("User deleted successfully."); } finally { session.close(); factory.close(); } } } |
Comparison Table: CRUD Operations
Operation | Method Used | Purpose |
---|---|---|
Create | session.save() | Adds a new record. |
Read | session.get() | Retrieves data by ID. |
Update | session.update() | Updates an existing record. |
Delete | session.delete() | Deletes a specific record. |
Output and Explanation
When you run the program:
- A database connection is established.
- The record with user_id = 3 is fetched.
- The record is deleted, and the transaction is committed.
Output:
1 |
User deleted successfully. |
Conclusion
In this article, we explored the Hibernate Delete Operation as part of CRUD operations. We implemented a complete example using the Users entity and App main program to delete a specific record from the database.
Key Takeaways
- Hibernate simplifies database operations.
- The session.delete() method is used for deleting records.
- Proper use of transactions ensures data integrity.