Featured Article

Spring Boot Hibernate Tutorial: Master ORM with Step by Step Guide

Kenneth Jul 13, 2026

Spring Boot and Hibernate are two powerful tools in the Java ecosystem, often used together for their robust functionality and seamless integration. Spring Boot simplifies Java application development, while Hibernate adds a layer of object-relational mapping (ORM) to your application. Let's dive into a comprehensive tutorial on how to use Spring Boot with Hibernate, step by step.

Spring Boot + Hibernate Tutorial and Example for Java Developers
Spring Boot + Hibernate Tutorial and Example for Java Developers

Before we proceed, ensure you have Java Development Kit (JDK) version 11 or later, Apache Maven, and your preferred Integrated Development Environment (IDE) like IntelliJ IDEA or Eclipse installed on your system.

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

Setting Up the Project

Our first goal is to set up a new Spring Boot project with the Spring Web and Spring Data JPA dependencies, which include Hibernate. We'll use Spring Initializr (start.spring.io) for this.

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

Select 'Maven Project', choose your Java version, and add 'Web' and 'JPA' dependencies. Then, download and import your project into your IDE.

Configuring the Application

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

Spring Boot comes with automatic configuration, but we need to tweak some settings for our Hibernate connection. Open the application.properties file in the src/main/resources folder.

Database Configuration

Add the following properties, replacing 'db_username', 'db_password', 'db_url', and 'db_name' with your actual database credentials:

How to Accept and Produce JSON as a response in Spring Boot? Example Tutorial
How to Accept and Produce JSON as a response in Spring Boot? Example Tutorial

spring.datasource.url=jdbc:mysql://db_url/db_name
spring.datasource.username=db_username
spring.datasource.password=db_password
spring.jpa.hibernate.ddl-auto=update

Entity Configuration

Create a new Java class, User.java, in the/com.example.demo package, and add the following content:

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

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
@RequiredArgsConstructor
public class User {
@Id
@GeneratedValue
private Long id;
private String name;
}

Implementing the Repository

How to Build Event-Driven Microservices with Spring Boot and Kafka? Example Tutorial
How to Build Event-Driven Microservices with Spring Boot and Kafka? Example Tutorial
a diagram showing the flow of data from an internet network to a business transaction process
a diagram showing the flow of data from an internet network to a business transaction process
10 Projects You Can Build to Learn Spring Boot in 2025
10 Projects You Can Build to Learn Spring Boot in 2025
Difference between @Mock and @MockBean in Spring Boot? Example Tutorial
Difference between @Mock and @MockBean in Spring Boot? Example Tutorial
My Favorite Courses to Learn Spring Boot in 2026 — Best of Lot
My Favorite Courses to Learn Spring Boot in 2026 — Best of Lot
Spring Boot H2 Database Example With Hibernate
Spring Boot H2 Database Example With Hibernate
Working with SQL Databases in Spring Boot Application - Dinesh on Java
Working with SQL Databases in Spring Boot Application - Dinesh on Java
5 Essential Spring Boot Annotations with Examples in Java - Tutorial
5 Essential Spring Boot Annotations with Examples in Java - Tutorial
What Is Spring Boot in Java? Beginner Guide 2026
What Is Spring Boot in Java? Beginner Guide 2026

Next, create an interface, UserRepository.java, extending Spring Data Jpa's JpaRepository. This interface will handle database operations for the User entity.

Repository Methods

Implement methods for creating, reading, updating, and deleting User entities. For example:

public interface UserRepository extends JpaRepository<User, Long> {
Optional<User> findByName(String name);
}

Creating the Service Layer

Create a service class, UserService.java, to encapsulate the business logic for user operations. This class will utilize the UserRepository interface for database interactions.

Here's a simple example:

import org.springframework.stereotype.Service;
import javax.transaction.Transactional;
import java.util.List;
@Service
public class UserService {
private final UserRepository userRepository;
public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}
@Transactional
public User saveUser(User user) {
return userRepository.save(user);
}
@Transactional(readOnly = true)
public List<User> getAllUsers() {
return userRepository.findAll();
}
}

Building the API

Finally, create a RestController to expose the user service as an API. Create a new class, UserController.java, and add the following code:

Here's a simple example:

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class UserController {
private final UserService userService;
public UserController(UserService userService) {
this.userService = userService;
}
@GetMapping("/users")
public List<User> getAllUsers() {
return userService.getAllUsers();
}
@PostMapping("/users")
public User saveUser(@RequestBody User user) {
return userService.saveUser(user);
}
}

Now you have a fully functional Spring Boot application using Hibernate for database operations. Run your application and test the API at http://localhost:8080/users.

Remember, this is just the beginning. You can enhance this application by adding error handling, validation, and authentication. Explore the vast libraries and tools in the Spring ecosystem to make your application more robust and maintainable. Happy coding!