When you’re building a Java application, you often need to store and retrieve data from a database. Writing this code manually can be repetitive and error-prone.
Luckily, Spring Data JPA and Hibernate can help. They allow you to easily interact with databases without writing too much SQL code. Let’s understand what they are and how to use them with Spring Boot.

๐ What is JPA?
JPA (Java Persistence API) is a Java specification that defines how to map Java objects to database tables. It’s like a set of rules or guidelines. But JPA itself does not do the work โ it needs an implementation like Hibernate.
โ๏ธ What is Hibernate?
Hibernate is a popular tool (implementation of JPA) that handles the actual interaction with the database. It converts Java classes into database tables and handles queries, inserts, updates, and deletes for you.
๐ฑ What is Spring Data JPA?
Spring Data JPA is part of the Spring ecosystem. It makes working with JPA and Hibernate even easier. Instead of writing SQL or even JPQL queries, Spring Data JPA lets you define simple methods, and it automatically creates the queries for you!
๐ ๏ธ Project Setup โ Step-by-Step
Weโll now create a small project using:
- Spring Boot
- Spring Data JPA
- Hibernate (used automatically)
- H2 in-memory database (for easy setup)
โ Step 1: Add Dependencies
Use this in your pom.xml
if using Maven:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> </dependencies> |
โ
Step 2: Add Configuration in application.properties
1 2 3 4 5 6 7 8 9 10 11 12 |
spring.datasource.url=jdbc:h2:mem:testdb spring.datasource.driverClassName=org.h2.Driver spring.datasource.username=sa spring.datasource.password= spring.jpa.database-platform=org.hibernate.dialect.H2Dialect spring.jpa.hibernate.ddl-auto=update spring.h2.console.enabled=true spring.jpa.show-sql=true |
ddl-auto=update
: Creates tables automatically from your Java classesshow-sql=true
: Prints SQL queries in the consoleh2.console.enabled=true
: Enables a browser-based console to check the database
๐ค Step 3: Create an Entity
An Entity is a Java class that represents a table in the database.
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 |
import jakarta.persistence.*; @Entity public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; private String email; // Constructors public User() {} public User(String name, String email) { this.name = name; this.email = email; } // Getters and setters // ... } |
Key Annotations:
@Entity
: Tells Spring this is a database entity@Id
: Primary key@GeneratedValue
: Auto-generates the ID
๐ Step 4: Create Repository
Repositories handle database operations.
1 2 3 4 5 6 7 8 9 |
import org.springframework.data.jpa.repository.JpaRepository; import java.util.List; public interface UserRepository extends JpaRepository<User, Long> { List<User> findByName(String name); // Automatically generates SQL } |
๐ง Step 5: Create a Service
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 |
import org.springframework.stereotype.Service; import java.util.List; @Service public class UserService { private final UserRepository userRepository; public UserService(UserRepository userRepository) { this.userRepository = userRepository; } public User saveUser(User user) { return userRepository.save(user); } public List<User> getUsers() { return userRepository.findAll(); } public void deleteUser(Long id) { userRepository.deleteById(id); } } |
๐ Step 6: Create a REST Controller
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 |
import org.springframework.web.bind.annotation.*; import java.util.List; @RestController @RequestMapping("/users") public class UserController { private final UserService userService; public UserController(UserService userService) { this.userService = userService; } @PostMapping public User createUser(@RequestBody User user) { return userService.saveUser(user); } @GetMapping public List<User> getAllUsers() { return userService.getUsers(); } @DeleteMapping("/{id}") public void deleteUser(@PathVariable Long id) { userService.deleteUser(id); } } |
๐งช Try It Out
- Run your application.
- Open browser and go to:
http://localhost:8080/h2-console
- JDBC URL:
jdbc:h2:mem:testdb
- JDBC URL:
- Send a
POST
request to/users
with JSON:
1 2 3 4 5 6 7 |
{ "name": "Alice", "email": "alice@example.com" } |
You can use Postman or cURL.
โจ Advantages of Using Spring Data JPA
- Minimal code โ no need to write SQL
- Auto table creation from Java classes
- Easy to use query methods (like
findByName
) - Seamless integration with Spring Boot
๐ Summary
With Spring Data JPA and Hibernate:
- You write less code
- You get a flexible and powerful way to manage databases
- You stay focused on building your app, not writing boilerplate