Simple definition#
- JPA (Jakarta Persistence) is the standard Java API for object-relational mapping: annotated entities are mapped to tables and managed by an
EntityManager. - Hibernate is the most widely used JPA implementation.
- Spring Data JPA sits on top and generates repositories, so you rarely write
EntityManagercode yourself.
The problem#
Hand-written JDBC means mapping rows to objects, tracking what changed and writing every INSERT and UPDATE yourself. ORMs automate that, but their automatic behaviours (caching, dirty checking, lazy loading, flush timing) surprise teams that do not understand the persistence context.
The persistence context#
The persistence context is the set of entity instances an EntityManager (a Hibernate Session) is currently managing. In Spring it usually lives for one transaction.
- First-level cache: within one context, loading the same ID twice returns the same Java instance, with no second SELECT.
- Unit of work: changes to managed entities are tracked and written at flush.
- Dirty checking: at flush, Hibernate compares each managed entity with its loaded snapshot and issues UPDATEs for changed ones. You do not need to call
save()to update a managed entity.
Entity states#
Open the interactive diagram: JPA entity states and the persistence context Standalone page with zoom, light/dark themes and SVG/PNG export, generated with Archify. Diagram source.
| State | Meaning | How you get there |
|---|---|---|
| Transient | A new object, not known to JPA | new Order() |
| Managed | Tracked by the current persistence context | persist, find, a query, merge result |
| Detached | Has an ID but its context is closed | After the transaction or clear(); returned to a web layer |
| Removed | Scheduled for deletion | remove(managed) |
Flush sends pending SQL to the database (still inside the transaction). It happens before commit, before queries whose results could be affected (FlushMode.AUTO), or when you call flush() explicitly. Commit makes it durable.
@Transactional
public void rename(long id, String name) {
Customer c = customers.findById(id).orElseThrow(); // managed
c.rename(name); // no save() needed: dirty checking issues the UPDATE at commit
}
Spring Data repositories#
public interface OrderRepository extends JpaRepository<Order, Long>, JpaSpecificationExecutor<Order> {
List<Order> findByCustomerIdAndStatusOrderByCreatedAtDesc(String customerId, Status status); // derived query
Optional<Order> findFirstByCustomerIdOrderByCreatedAtDesc(String customerId);
@Query("select o from Order o join fetch o.lines where o.id = :id") // JPQL
Optional<Order> findWithLines(long id);
<T> List<T> findByStatus(Status status, Class<T> type); // dynamic projection
@Modifying
@Query("update Order o set o.status = :to where o.status = :from and o.createdAt < :before")
int bulkUpdate(Status from, Status to, Instant before); // bypasses the persistence context!
}
public record OrderSummary(Long id, BigDecimal total) {} // DTO projection: only the selected columns
- Derived queries come from method names. Keep them short; use
@Querybeyond about three conditions. - Projections (interface or record/DTO) fetch only the columns you need, which is great for read endpoints.
save()callspersistfor new entities andmergefor detached ones. Merge copies state into a managed instance and returns it, so use the returned object.- Bulk
@Modifyingqueries skip dirty checking, lifecycle callbacks and the first-level cache. Clear the context afterwards (clearAutomatically = true).
Identifier generation#
| Strategy | Notes |
|---|---|
SEQUENCE (with allocationSize 50) | Best with Hibernate: allows JDBC batching and pooled IDs |
IDENTITY (auto-increment) | Simple, but disables insert batching |
UUID (@UuidGenerator, time-ordered UUIDv7 in recent Hibernate) | Good for distributed systems; prefer time-ordered UUIDs for index locality |
Performance essentials#
- JDBC batching:
spring.jpa.properties.hibernate.jdbc.batch_size=50,order_inserts=trueandorder_updates=true. - Read-only transactions:
@Transactional(readOnly = true)skips dirty checking and snapshots. - Large result sets: stream (
Stream<T>with a fetch size), paginate with keyset pagination, or use projections. - Open Session in View: Spring Boot enables
spring.jpa.open-in-view=trueby default and logs a warning. It keeps the session open during view rendering, which hides lazy-loading problems and holds connections longer. Set it tofalsefor APIs and fetch what you need explicitly. - See the N+1 problem and fetch strategies.
Spring Data JPA vs alternatives#
| Option | When |
|---|---|
| Spring Data JPA | Rich domain models, CRUD plus moderate queries |
| Spring Data JDBC | Simpler aggregates, no lazy loading or persistence-context magic |
| jOOQ / JdbcClient | SQL-first, complex reporting queries, full control |
| R2DBC | Reactive stacks |
Common mistakes#
Quick check#
What is the persistence context? The set of managed entity instances for an EntityManager or Session, usually one per transaction. It is a first-level cache and a unit of work with dirty checking.
Why does modifying a managed entity update the database without save()?
At flush, dirty checking compares managed entities with their snapshots and issues UPDATE statements.
What are the four entity states? Transient, managed, detached and removed.
persist vs merge?
persist makes a new instance managed. merge copies a detached instance’s state onto a managed instance and returns that managed copy.
Why disable open-session-in-view? It extends database sessions into the web layer, hides lazy-loading and N+1 problems, and holds connections longer.
Summary#
- Spring Data repositories sit on JPA and Hibernate; the persistence context is the key concept.
- Managed entities are dirty-checked and flushed automatically; know your entity states.
- Use projections, batching, sequences and read-only transactions; disable open-in-view.
- Spring Boot 4 brings Hibernate 7 and Jakarta Persistence 3.2.
References#
- Spring Data JPA, reference documentation.
- Hibernate ORM, User Guide and Hibernate 7 migration guide.
- Vlad Mihalcea, High-Performance Java Persistence.