🗄️

Spring Data JPA & Hibernate Mastery

ORM from first principles, Hibernate internals, Spring Data JPA, query optimization, N+1 fixes, caching, concurrency, and production-grade persistence-layer engineering.

Practice interview questions on this topic →
1

Why ORM Exists — Persistence Fundamentals & the Impedance Mismatch

beginner

Before any JPA annotation or Hibernate feature makes sense, you need to feel the actual problem they solve: the structural mismatch between how Java thinks (objects) and how a database thinks (tables).

2

JPA Fundamentals — The Specification, Providers & Architecture

beginner

JPA is a specification, not an implementation — understanding the split between the standard contract and Hibernate's concrete implementation of it is the vocabulary the rest of this course depends on.

3

Hibernate Architecture — SessionFactory, Session, EntityManager

beginner

Every Hibernate operation flows through one of two objects with a deliberately asymmetric cost: an expensive, one-time factory, and a cheap, per-request session — understanding this split is foundational to everything from here on.

4

The Persistence Context — First-Level Cache, Dirty Checking & Flush

intermediate

The single most important internal concept in Hibernate — it explains why an entity mutation persists with no explicit save call, why repeated loads return the same object, and what a flush actually does.

5

Entity Lifecycle — Persist, Merge, Detach, Remove & Proxies

intermediate

Every entity is always in exactly one of four states — knowing which one, and how persist/merge/detach/remove transition between them, prevents the majority of real-world Hibernate bugs, LazyInitializationException chief among them.

6

Spring Data JPA — Repositories, Derived Queries & Auto-Configuration

beginner

This is where the raw JPA/Hibernate concepts from the first five chapters meet the everyday tool real Spring Boot applications actually use — repositories generate a working implementation from just an interface, with derived query methods parsing the method name itself into a real query.

7

JPQL, Native Queries, Specifications & Criteria API

intermediate

Four distinct tools for querying beyond what a derived method name can express, each suited to a different problem shape — from portable entity-oriented JPQL to fully dynamic, runtime-built Specifications.

8

Projections, Pagination & Sorting

intermediate

Loading full entities and unbounded result sets is wasteful by default — projections fix over-fetching in shape, pagination fixes it in size, and Page/Slice/Stream trade off cost against what information you actually need.

9

Entity Mapping — @Entity, @Id, Inheritance & Composite Keys

intermediate

Every entity you've used in this course had to be correctly mapped first — this chapter covers that mapping directly, including the genuinely hard cases (inheritance, composite keys) that have no single obvious relational equivalent.

10

Relationships — @OneToMany, @ManyToOne, @ManyToMany, Cascade & FetchType

intermediate

Every relationship in your entity model requires a deliberate answer to "what happens to the children when the parent changes" and "should this load immediately or on demand" — getting these two decisions wrong is the most common source of both accidental cascading deletes and N+1 query problems.

11

Transactions — @Transactional, Propagation & Isolation

advanced

Every persistence operation in this course happens inside a transaction — this chapter covers exactly how @Transactional creates and manages it, and the two sharp edges (self-invocation, checked-exception rollback) that surprise most developers at least once.

12

N+1 Queries & Fetching Strategies — JOIN FETCH, Entity Graph, Batch Fetch

advanced

The single most common real-world Hibernate performance problem, and the highest-leverage skill in this entire course to actually master — one query for N parents, plus N more for each one's lazily-loaded association, fixed by JOIN FETCH, Entity Graphs, or batch fetching.

13

Caching — Second-Level Cache, Query Cache & Redis

advanced

The first-level cache only lives for one request — the second-level cache and query cache share data across requests and application instances, trading some staleness risk for real, measurable database-load reduction.

14

Concurrency — Optimistic & Pessimistic Locking, MVCC

advanced

Two concurrent transactions touching the same row is unavoidable in any real application — @Version-based optimistic locking and JPA's pessimistic lock modes are the concrete tools for handling that collision correctly.

15

Production Engineering — HikariCP, SQL Logging & Hibernate Statistics

advanced

Every technique from every earlier chapter is only verifiable in a real running application through the specific visibility tools this chapter covers — HikariCP tuning, SQL logging, and Hibernate statistics are your dashboard for an otherwise invisible persistence layer.

16

Spring Boot Integration — Flyway/Liquibase, Auditing & Multi-Database Setups

advanced

How a schema actually evolves safely over a real application's lifetime, plus two other everyday integration concerns: automatic auditing and running against more than one database at once.

17

Microservices Persistence — Database-per-Service, Outbox Pattern & CDC

advanced

Database-per-service breaks every cross-entity transactional guarantee this course has relied on so far — the outbox pattern and CDC are the specific tools that restore reliable consistency across independently-owned service databases.

18

Testing the Persistence Layer — @DataJpaTest & Testcontainers

advanced

Verifying persistence-layer behavior against a mock, or an embedded database that isn't your real production engine, can pass a test while completely missing what actually happens in production — @DataJpaTest and Testcontainers are how you test against the real thing, reliably and automatically.

19

System Design for the Persistence Layer — Read Replicas, Sharding & CQRS

advanced

A single database instance eventually hits a real ceiling no amount of query tuning can fix — read replicas, sharding, and CQRS are the three deliberate architectural strategies for scaling a persistence layer past that point, each with a genuinely different cost and consistency tradeoff.

20

Debugging & Troubleshooting — LazyInitializationException & Common Production Failures

advanced

The capstone troubleshooting chapter — a fast, symptom-to-cause map back into every earlier chapter's mechanism, for the moment you're actually debugging a real, live production persistence-layer issue and need to move fast.

21

Spring Data JDBC vs. Spring Data JPA

advanced

Spring Data JPA isn't the only Spring Data persistence option — Spring Data JDBC deliberately removes the persistence context entirely, trading JPA's automatic convenience for a smaller, more predictable, fully explicit model.

22

The ORM Landscape — Hibernate vs. MyBatis vs. jOOQ

advanced

Hibernate is one philosophy among several real, valid options — MyBatis and jOOQ solve the same underlying problem with genuinely different tradeoffs, worth knowing well enough to make an informed choice rather than defaulting to Hibernate by habit.

23

JPA vs. Raw JDBC — Performance & When to Drop Down

advanced

JPA's entity-hydration overhead is real but usually negligible — this chapter is about recognizing the specific, narrow cases (extremely hot, read-only, high-volume paths) where dropping to a projection or raw JDBC is a measured, surgical win, not a wholesale rejection of everything this course covers.

24

Hibernate 6.x & Spring Boot 3.x — What's New, What Changed

intermediate

The fundamentals from every earlier chapter are unchanged — but the javax→jakarta namespace migration, a new query translation engine, and native JSON support are genuine, practical changes worth knowing explicitly rather than carrying over outdated Hibernate 5 assumptions.

25

Multi-Tenancy in Hibernate — Schema, Database & Discriminator Strategies

advanced

A SaaS application serving multiple tenants needs a deliberate, structurally-enforced isolation strategy — Hibernate has first-class support for three distinct approaches, each with a genuinely different isolation-versus-operational-cost tradeoff.

26

Soft Deletes — @SQLDelete, @SQLRestriction/@Where

intermediate

Hibernate's dedicated soft-delete annotations move the pattern from something every developer must remember per query into something the ORM enforces automatically, everywhere, by default.

27

Bulk Updates & Batch Processing

advanced

Updating or inserting thousands of rows one entity at a time through the persistence context is catastrophically slow at scale — bulk JPQL statements and JDBC batching are the two distinct tools for avoiding that cost, each solving a genuinely different shape of problem.

28

Stored Procedures with JPA

advanced

Some logic genuinely belongs inside the database as a stored procedure — @NamedStoredProcedureQuery and StoredProcedureQuery let you call one cleanly through JPA's parameter-binding and result-mapping conventions, without dropping to raw JDBC just for that one call.

29

JSON Columns — PostgreSQL JSONB & MySQL JSON with Hibernate

advanced

Not every field has a fixed shape at design time — JSON columns are the deliberate escape hatch for genuinely variable, evolving data, now natively supported by Hibernate 6 without needing a third-party conversion library.

Coming soon to Spring Data JPA & Hibernate Mastery

This section keeps growing — here's what's planned next.

  • Spring Data JDBC — a lighter-weight alternative to full JPA
  • Reactive Persistence with Spring Data R2DBC
  • Multi-Tenancy Strategies in Hibernate
  • GraphQL + JPA Integration Patterns
  • Database Migration Strategies at Scale (zero-downtime schema changes)