Featured Article

Spring Boot JPA Hibernate Tutorial: Master ORM with Step-by-Step Examples

Kenneth Jul 13, 2026

Spring Boot, with its comprehensive feature set and ease of use, has become a popular choice for Java developers. When it comes to working with databases, Spring Boot integrates seamlessly with JPA and Hibernate, providing a robust and consistent data access layer. In this tutorial, we will embark on a journey to explore how to use Spring Boot with JPA and Hibernate, ensuring a smooth and efficient development experience.

Spring Boot MapStruct Example of Mapping JPA and Hibernate Entity to DTO | Spring Boot Tutorial
Spring Boot MapStruct Example of Mapping JPA and Hibernate Entity to DTO | Spring Boot Tutorial

Before we dive into the details, let's ensure you have the necessary prerequisites. You should have Java Development Kit (JDK) version 8 or later installed, along with Apache Maven or Spring Boot's integrated Gradle support. Additionally, you'll need a functioning database, either MySQL, PostgreSQL, or any other compatible database. We'll use MySQL for our examples.

Spring Boot, MySQL, JPA, Hibernate Restful CRUD API Tutorial
Spring Boot, MySQL, JPA, Hibernate Restful CRUD API Tutorial

Setting Up the Project with Spring Boot and JPA

Let's start by creating a new Spring Boot project using Spring Initializr (https://start.spring.io/). For this tutorial, we'll use the following dependencies: Web, JPA, and MySQL Driver. Once the project is set up, we can proceed to configure the database connection.

Spring Boot H2 Database Example With Hibernate
Spring Boot H2 Database Example With Hibernate

Open your application.properties file and add the following configuration to connect to your MySQL database:

  1. spring.datasource.url=jdbc:mysql://localhost:3306/mydb
  2. spring.datasource.username=myuser
  3. spring.datasource.password=mypassword
  4. spring.jpa.hibernate.ddl-auto=update
Top 7 Spring and Hibernate Training Courses for Java JEE Programmers in 2025 - Best of Lot
Top 7 Spring and Hibernate Training Courses for Java JEE Programmers in 2025 - Best of Lot

Creating a JPA Entity

Now, let's create a simple JPA entity named User. Inside the model package, create a new class named User.java with the following content:

import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;
import javax.persistence.Table;import lombok.AllArgsConstructor;
@Entity @Table(name = "users") @AllArgsConstructor public class User
{ @Id @GeneratedValue private Long id; private String name; private String email; }
// getters and setters
RESTful API Example with Spring Data REST, and JPA Hibernate Many To Many Extra Columns |Spring Boot
RESTful API Example with Spring Data REST, and JPA Hibernate Many To Many Extra Columns |Spring Boot

Creating a Repository Interface

Next, we'll create a JPA repository interface for CRUD operations. Inside the repository package, create a new interface named UserRepository.java with the following content:

import org.springframework.data.jpa.repository.JpaRepository;import org.springframework.stereotype.Repository;
@Repository public interface UserRepository extends JpaRepository
What Is Spring Boot in Java? Beginner Guide 2026
What Is Spring Boot in Java? Beginner Guide 2026

Implementing CRUD Operations with Spring Data JPA

Now that we have our JPA entity and repository set up, let's implement the CRUD (Create, Read, Update, Delete) operations using Spring Data JPA's default methods. Create a new service class named UserService.java in the service package with the following content:

Top 50 Hibernate and JPA Interview Questions Answers for 3 to 5 Years Experienced Java developers
Top 50 Hibernate and JPA Interview Questions Answers for 3 to 5 Years Experienced Java developers
How to Enable logging of SQL statements in Spring Boot? Hibernate + JPA Example Tutorial
How to Enable logging of SQL statements in Spring Boot? Hibernate + JPA Example Tutorial
Spring Boot cheatsheet - Masterspringboot
Spring Boot cheatsheet - Masterspringboot
Top 15 Spring Boot Interview Questions with Answers for Java/JEE Programmers
Top 15 Spring Boot Interview Questions with Answers for Java/JEE Programmers
Top 10 Free Courses to learn Spring Framework and Spring Boot in 2024— Best of Lot
Top 10 Free Courses to learn Spring Framework and Spring Boot in 2024— Best of Lot
Create Cricket Stats API App Using Spring Boot
Create Cricket Stats API App Using Spring Boot
Difference between Hibernate, JPA, and Spring Data JPA?
Difference between Hibernate, JPA, and Spring Data JPA?
Spring Boot + JPA + Hibernate + Oracle
Spring Boot + JPA + Hibernate + Oracle
Spring Boot 4, Spring 7 & Hibernate for Beginners
Spring Boot 4, Spring 7 & Hibernate for Beginners

Creating a User

To create a new user, add the following method to the UserService class:

public User create(User user) {
User savedUser = userRepository.save(user);
return savedUser;
}

Fetching All Users

To fetch all users, add the following method to the UserService class:

public List<User> getAllUsers(){
return userRepository.findAll();
}

Updating a User

To update an existing user, add the following method to the UserService class:

public User update(Long id, User updatedUser) {
User existingUser = userRepository.findById(id).orElseThrow(()) => new RuntimeException("User not found"));
existingUser.setName(updatedUser.getName());// Assuming fields can be updated
existingUser.setEmail(updatedUser.getEmail());
return userRepository.save(existingUser);
}

Deleting a User

To delete a user, add the following method to the UserService class:

public void delete(Long id){
userRepository.deleteById(id);
}

By utilizing Spring Data JPA's default methods and following the CRUD repository pattern, we've implemented the CRUD operations with minimal coding effort. This demonstrates the power and convenience of using Spring Boot with JPA and Hibernate.

In conclusion, we've explored how to set up a Spring Boot project with JPA, created a JPA entity and repository, implemented CRUD operations using Spring Data JPA, and connected it all to a MySQL database. This foundation will enable you to build more advanced applications and explore other features of Spring Boot, JPA, and Hibernate. Happy coding! 🚀