Hibernate CRUD Operation – Read
Table of Contents
- Introduction
- Understanding Hibernate CRUD Operations
- Read Operation in Hibernate
- Code Walkthrough
- Comparison Table
- Conclusion
Introduction
Hibernate is a powerful ORM (Object-Relational Mapping) framework that simplifies database interactions in Java. CRUD operations—Create, Read, Update, and Delete—are fundamental in any database-driven application.
This article focuses on “Read Operation” in Hibernate, explaining its significance, syntax, and implementation using a step-by-step approach.
Operation | Purpose | Description |
---|---|---|
Create (C) | Add new records | Inserts data into the database |
Read (R) | Fetch existing records | Retrieves data using unique identifiers |
Update (U) | Modify existing records | Updates specified records in the database |
Delete (D) | Remove existing records | Deletes specified records from the database |
Understanding Hibernate CRUD Operations
What is CRUD?
CRUD refers to the four basic database operations—Create, Read, Update, and Delete—performed on persistent data in any application.
Hibernate’s Role
Hibernate provides a simplified approach to handle CRUD operations by abstracting JDBC code and managing database entities through mapping.
Read Operation in Hibernate
The Read Operation in Hibernate involves retrieving data using the primary key or custom queries. Hibernate simplifies fetching records with minimal code.
Steps to Perform a Read Operation
- Create a SessionFactory.
- Open a Session.
- Begin a Transaction.
- Fetch the Entity using session.get() method.
- Commit the Transaction.
Code Implementation
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 |
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(); } @Override public String toString() { return "Users [userId=" + userId + ", username=" + username + ", firstname=" + firstname + ", lastname=" + lastname + "]"; } } |
Main Program – 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 |
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) { // Step 1: Create a SessionFactory SessionFactory factory = new Configuration() .configure("hibernate.cfg.xml") .addAnnotatedClass(Users.class) .buildSessionFactory(); // Step 2: Open a Session Session session = factory.getCurrentSession(); try { // Step 3: Start a transaction session.beginTransaction(); // Step 4: Read operation - Fetch data using Primary Key Users user = session.get(Users.class, 1); // Step 5: Commit the transaction session.getTransaction().commit(); // Print the fetched user System.out.println(user); } finally { session.close(); factory.close(); } } } |
Code Walkthrough
- Entity Class: Maps Java class to the database table.
- Main Class: Retrieves a record using the primary key with session.get().
Output
1 |
Users [userId=1, username=john_doe, firstname=John, lastname=Doe] |
Comparison Table
Method | Purpose | Returns |
---|---|---|
session.get() | Fetch data by Primary Key | Entity object or null if not found |
session.load() | Fetch data lazily by Proxy | Proxy object; exception if not found |
Conclusion
The Read Operation in Hibernate simplifies retrieving records from the database. By leveraging methods like session.get(), developers can fetch specific entities with minimal effort.