Sub topic 1
What JPA, Hibernate and Spring Data JPA each do
Prerequisites
This chapter assumes you know core Java (classes, interfaces, generics, annotations) and basic SQL (tables, primary keys, foreign keys, joins). You should also know the basics of Spring Boot: beans, dependency injection and application properties.
The problem: objects versus tables
Java code works with objects that reference each other. A relational database stores rows in tables that are linked by foreign keys. Moving data between the two by hand with JDBC means writing SQL strings, reading a ResultSet column by column, and copying values into objects. This is repetitive and easy to get wrong.
Object-relational mapping (ORM) removes that work. An ORM maps classes to tables and fields to columns. It generates the SQL and converts result rows into objects.
Three layers, three jobs
Three names are often used as if they meant the same thing. They are separate layers.
| Layer | What it is | What it does |
|---|---|---|
| JPA (Jakarta Persistence) | A specification | Defines the annotations (`@Entity`, `@Id`), the `EntityManager` API and the JPQL query language |
| Hibernate | An implementation of JPA | Does the work: generates SQL, tracks changes, manages loading and caching |
| Spring Data JPA | A Spring library on top of JPA | Generates repository implementations from interfaces, so you write less data access code |
A specification is only a set of rules and interfaces. It does nothing until a provider implements it. In a Spring Boot project the provider is Hibernate by default.
Seeing each layer in code
The first snippet uses only JPA annotations. They come from the jakarta.persistence package and describe the mapping. Hibernate reads them at runtime.
import jakarta.persistence.*;
@Entity
@Table(name = "books")
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false)
private String title;
protected Book() {
}
public Book(String title) {
this.title = title;
}
public Long getId() { return id; }
public String getTitle() { return title; }
}The next snippet is Spring Data JPA. You declare an interface and write no implementation.
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
public interface BookRepository extends JpaRepository<Book, Long> {
List<Book> findByTitleContainingIgnoreCase(String text);
}At startup Spring creates a proxy that implements this interface. It provides save, findById, findAll, delete and more. It also turns the method name findByTitleContainingIgnoreCase into a query. Internally the proxy calls the JPA EntityManager, and Hibernate turns those calls into SQL.
Common mistakes
- Treating Spring Data JPA as an ORM. It does not map anything. Mapping and SQL generation belong to JPA and Hibernate.
- Importing
javax.persistenceannotations. Spring Boot 3 and later usejakarta.persistence. The old package is silently ignored or fails to compile, depending on the dependencies. - Assuming Hibernate is the only choice. JPA allows other providers, but Hibernate is the default in Spring Boot.
Exercise
A teammate says, "We use Spring Data JPA, so Spring Data JPA generates our SQL." Which layer actually generates the SQL, and what does Spring Data JPA contribute?
Show answer
Hibernate, as the JPA provider, generates the SQL. Spring Data JPA contributes the repository implementation. It creates a proxy for your interface, supplies CRUD methods, and turns method names or @Query declarations into calls on the JPA EntityManager.
Exercise
Which of these is a specification and not an implementation: JPA, Hibernate or Spring Data JPA?
Show answer
JPA. It defines annotations, the EntityManager API and JPQL. Hibernate implements it, and Spring Data JPA builds on top of it.