VOOZH about

URL: https://www.geeksforgeeks.org/advance-java/crud-operations-using-hibernate/

⇱ CRUD Operations using Hibernate - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

CRUD Operations using Hibernate

Last Updated : 23 Apr, 2026

Hibernate is a Java Object-Relational Mapping (ORM) framework that simplifies database interactions by mapping Java objects to relational tables. It allows developers to perform CRUD operations (Create, Read, Update, Delete) without writing complex SQL queries.

CRUD refers to database operations:

  • C -> Insert new records into the database
  • R -> Retrieve records from the database
  • U -> Modify existing records
  • D -> Remove records from the database

Steps to Implementation of CRUD Operations using Hibernate

Steps to implement CRUD operations in Hibernate involve configuring Hibernate, creating an entity class, setting up a SessionFactory, and performing create, read, update, and delete operations using Hibernate sessions.

Step 1: Create a Maven Project

Project Structure

👁 1
Structure

Step 2: Configure Hibernate

Create a Hibernate configuration file hibernate.cfg.xml inside src/main/resources.

Note: Make sure your MySQL database student_info exists.

Step 3: Create SessionFactory Provider

Hibernate uses a SessionFactory to create sessions for database operations.

Create a class SessionFactoryProvider.java:

Step 4: Create the Entity (POJO) Class

Create a class Student.java in the beans package:

Step 5: Create a Record (Create Operation)

Create Create.java:

Run and Verify

  1. Run Create.java ->This code will insert the new record into the student_info table.
👁 student table
output

Step 6: Retrieve a Record (Read Operation)

Create Retrieve.java: Retrieving data from the database:

The following details will be fetched from the database:

👁 Student record

RetrieveUsingGet.java : This example demonstrates fetching data using get() method.

Output:

👁 get() vs load()

Step 7: Update a Record (Update Operation)

Create Update.java: Updating a record in the database:

Run and Verify

The following record will be updated:

👁 Student table

Step 8: Delete a Record (Delete Operation)

Create Delete.java: To delete an object from the database, the session.delete() method is used.

Run and Verify

After running the application check record into database:

👁 Student table
output
Comment
Article Tags:

Explore