Hibernate CRUD Operations – “Create” in Action
Table of Contents
- Introduction
- Understanding the “Create” Operation
- Setting Up the Environment
- Implementation of “Create” 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 “Create Operation” in Hibernate.
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 the “Create” Operation
Core Components of Hibernate:
- Session – Interface to interact with the database.
- Transaction – Manages consistency.
- Entity – Java class mapped to a database table.
Basic Syntax
1 2 3 4 5 6 7 8 9 |
Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); YourEntity entity = new YourEntity(); entity.setProperty("value"); session.save(entity); transaction.commit(); session.close(); |
Setting Up the Environment
1. Hibernate Configuration
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 |
package org.studyeasy.entity; import javax.persistence.Entity; import javax.persistence.Id; @Entity public class Users { @Id private int id; // Primary Key private String name; // User's Name private String email; // User's Email public Users() { } public Users(int id, String name, String email) { this.id = id; this.name = name; this.email = email; } // Getters and Setters 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. 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 |
package org.studyeasy; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; 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.openSession(); try { Transaction transaction = session.beginTransaction(); Users user = new Users(1, "John Doe", "john@example.com"); session.save(user); transaction.commit(); System.out.println("New User added successfully!"); } finally { session.close(); factory.close(); } } } |
Implementation of “Create” Operation
When executed, the program outputs:
1 |
New User added successfully! |
Database Table:
ID | Name | |
---|---|---|
1 | John Doe | john@example.com |
Comparison Table
Operation | Method | Action |
---|---|---|
Create | session.save() | Adds data |
Update | session.update() | Modifies data |
Read | session.get() | Fetches data |
Delete | session.delete() | Removes data |
Conclusion
We learned how to implement the “Create Operation” in Hibernate using Java. By following the steps above, developers can easily insert new records into the database.