ORM and JPA series: 1. Entities and repositories · 2. Entity relationships · 3. The E-Learning schema, DTOs, JSON · 4. Persistence context and N+1 · 5. JPQL, @Transactional, locking
Once entities point at each other (part 2, part 3), Hibernate does a lot behind your back: a changed field becomes an UPDATE without save, an endpoint listing 50 courses fires 51 SQL statements, and LazyInitializationException shows up out of nowhere. After this post you can explain the persistence context, dirty checking and flushing, and you can detect and fix N+1. Read part 1 (entities, repositories) and part 2 (entity relationships) first.
1. The persistence context and entity lifecycle
Key idea: the persistence context is where Hibernate keeps the entities it manages during a transaction. Any change to a managed entity is written to the database automatically on flush.
The slides don’t cover this, but senior interviews often do. The examples use the Course, Teacher and Tag entities from part 2.
1.1. What the persistence context is
The persistence context is a memory area tied to an EntityManager (a Session in Hibernate terms), essentially a map from (entity type, id) to object. In Spring, by default it lives as long as one transaction. It guarantees two things:
- First-level cache: calling
findById(1)twice in the same transaction runs one SQL statement; the second call is served from the context. - Identity: within one context, a database row maps to exactly one object, so
em.find(Course.class, 1) == em.find(Course.class, 1)istrue.
1.2. The four entity states
stateDiagram-v2
[*] --> Transient: new Course()
Transient --> Managed: persist()
Managed --> Removed: remove()
Removed --> Managed: persist()
Managed --> Detached: transaction ends / clear() / detach()
Detached --> Managed: merge() (returns a new managed copy)
Removed --> [*]: flush + commit (DELETE)
[*] --> Managed: find() / query
The four states: does it have an id, is it tracked
| State | Has an id? | Tracked by the context? | Notes |
|---|---|---|---|
| Transient (new) | Usually not | No | A plain Java object, unrelated to the database |
| Managed (persistent) | Yes | Yes | Every change is written automatically on flush |
| Detached | Yes | No | The context is closed; changing it does nothing |
| Removed | Yes | Yes, scheduled for deletion | The DELETE runs on flush |
persist(entity): makes that very object managed.merge(entity): copies the state of the (usually detached) object onto a managed instance (loading it if needed) and returns that instance. The argument stays detached. A common bug: keep modifying the original object after merging.
1.3. Dirty checking and flushing
When an entity is loaded, Hibernate keeps a snapshot (a copy of its initial state). On flush, it compares each field with the snapshot and issues an UPDATE for anything that changed. That’s dirty checking.
@Transactional
public void rename(long courseId, String newTitle) {
Course course = courseRepository.findById(courseId).orElseThrow();
course.rename(newTitle);
// No save() needed: course is managed, so on commit Hibernate flushes
// and issues UPDATE courses SET title=? ... on its own
}
Flush is not commit. A flush sends SQL to the database but stays inside the current transaction; only the commit makes it durable and visible to others. A rollback after a flush still undoes everything.
- Calling
save()on a managed entity is redundant (harmless, but misleading). - Accidentally changing a field on a managed entity (say, to “format” it for the response) is a real database change. One more reason to map to DTOs early.
When flushes happen, updating every column, and the cost for big jobs
With the default FlushMode.AUTO, a flush happens:
- Before the transaction commits.
- Before running a query that could be affected by pending changes (so the query sees them).
- When you call
flush()orsaveAndFlush()explicitly.
The exception: with GenerationType.IDENTITY, persist must INSERT immediately to get the id, without waiting for a flush, so Hibernate also can’t group those INSERTs into a JDBC batch. The common errors post shows a unique-constraint error thrown by save() rather than flush().
Loading 10,000 entities in one transaction means 10,000 snapshots on the heap and a flush that compares 10,000 objects. For big jobs, work in chunks with flush() + clear() after each chunk, or use @Transactional(readOnly = true) when only reading (Hibernate then skips snapshots and dirty checking).
2. Lazy, eager and the N+1 problem
Key idea: keep associations LAZY and fetch exactly what each use case needs. N+1 is the price of touching lazy associations in a loop; fix it by loading the association in batches or in the same SQL statement.
2.1. Lazy vs eager
- EAGER: loading the entity loads the association too (via a JOIN or an extra query).
- LAZY: the association is replaced by a proxy (a stand-in object, for
@ManyToOne) or a wrapper collection (PersistentBag,PersistentSetfor@OneToMany). Hibernate runs the query only when you access the data.
LazyInitializationException: you touched a lazy association after the persistence context closed, e.g. Jackson serializing course.getTeacher().getName() in the controller. The right fix is to fetch what you need inside the service (fetch join, entity graph, DTO projection), not to switch to EAGER or enable open-in-view (see part 5 of the series).
This bug often slips past tests: @Transactional on a test keeps the session open until the test ends, so lazy loading still works there while production throws; see the Spring Boot testing post.
2.2. What N+1 is
@Transactional(readOnly = true)
public List<CourseSummary> listCourses() {
return courseRepository.findAll().stream() // 1 query: SELECT * FROM courses
.map(c -> new CourseSummary(c.getTitle(),
c.getTeacher().getName())) // N queries: SELECT * FROM teacher WHERE id=?
.toList();
}
Each course triggers one more query for its teacher: N+1 queries, so 50 rows means 51 database round trips. It’s fast on localhost so nobody notices; in production, with network latency and concurrent traffic, it falls over.
How to catch it: log SQL in development, turn on hibernate.generate_statistics=true, write tests that assert the statement count (datasource-proxy or Hypersistence Utils make that easy).
2.3. How to fix it
1. Fetch join in JPQL: load the association in the same SQL statement with a JOIN.
@Query("select c from Course c join fetch c.teacher where c.status = :status")
List<Course> findWithTeacherByStatus(@Param("status") CourseStatus status);
2. @EntityGraph: declare which associations to fetch without writing JPQL; works with derived queries.
@EntityGraph(attributePaths = {"teacher", "tags"})
List<Course> findByStatus(CourseStatus status);
3. Batch fetching: still lazy, but many ids are grouped into one WHERE id IN (...). N+1 becomes 1 + ceil(N/batch). Worth enabling by default as a safety net, or put @BatchSize(size = 50) on specific collections/entities.
spring.jpa.properties.hibernate.default_batch_fetch_size: 50
4. DTO projection: select only the columns you need, with no entities and no dirty checking. The fastest option for read-only screens.
public record CourseSummary(Long id, String title, String teacherName) {}
@Query("""
select new com.vinhnq.elearning.dto.CourseSummary(c.id, c.title, t.name)
from Course c join c.teacher t
where c.status = :status
""")
List<CourseSummary> findSummaries(@Param("status") CourseStatus status);
Two more fetch-join gotchas with collections
MultipleBagFetchException: fetch-joining twoListcollections in one query. Switching toSetsilences the error but produces a Cartesian product (10 tags × 100 enrolments = 1,000 rows for one course). Fetch each collection in its own query.
Exercises
Exercise 6 (extra practice, not in the slides): count and fix the N+1 in the course list API
Task: GET /courses returns each course’s name with its teacher’s username. Write a test proving the current code has an N+1, then fix it so the number of SQL statements doesn’t grow with the number of courses.
Solution to Exercise 6: a query-count test and measurements on Hibernate 6.6
Turn on Hibernate statistics (spring.jpa.properties.hibernate.generate_statistics=true) and count prepared statements:
@SpringBootTest
@Transactional
class CourseListQueryCountTest {
@Autowired CourseRepository courseRepository;
@Autowired EntityManagerFactory emf;
@Test
void listing_courses_uses_a_constant_number_of_queries() {
Statistics stats = emf.unwrap(SessionFactory.class).getStatistics();
stats.clear();
courseRepository.findAllWithTeacher() // the fixed query
.forEach(c -> c.getTeacher().getUsername());
assertThat(stats.getPrepareStatementCount()).isEqualTo(1);
}
}
public interface CourseRepository extends JpaRepository<Course, Long> {
@Query("select c from Course c join fetch c.teacher")
List<Course> findAllWithTeacher();
}
I ran the same mapping on Hibernate 6.6 + H2 with 50 courses, each with its own teacher:
| How the data is loaded | SQL statements |
|---|---|
from Course, then getTeacher().getUsername() in a loop |
51 |
Same, with default_batch_fetch_size = 50 |
2 |
join fetch c.teacher |
1 |
The loop touches each course’s teacher proxy, one SELECT ... FROM teacher WHERE id = ? at a time. The fetch join loads both tables in one JOIN; batch fetching stays lazy but groups 50 ids into one IN (...) query.
What a senior would add: return a CourseSummary (DTO projection) for read-only screens; keep a query-count test on every important list endpoint so the N+1 doesn’t sneak back after a refactor; enable default_batch_fetch_size project-wide as a safety net.
Remember
- Can you explain the two guarantees of the persistence context, and how long it lives in Spring?
- Do you know why changing the object after
mergedoes nothing, and whysave()with an assigned id adds a SELECT? - Can you explain why changing an entity without
savestill produces an UPDATE, and how a flush differs from a commit? - Can you name the four N+1 fixes and when to pick each?
- Do you know why a collection
join fetchwithPageableis dangerous, and what to do instead?
Interview questions
Junior
What is JPA’s first-level cache?
It’s the persistence context tied to one EntityManager, which in Spring usually lives for one transaction.
Within that transaction a second findById(1) runs no SQL and returns the object already loaded, and one database row always maps to exactly one Java object. It isn’t shared across transactions or requests; a cache shared between requests is the second-level cache, a different thing you have to enable explicitly.
How is a flush different from a commit?
A flush sends pending SQL to the database but stays inside the transaction; a commit ends the transaction and makes the changes durable.
After a flush, other transactions can’t see the changes yet and a rollback still undoes them. Hibernate flushes automatically before commit and before queries that pending changes could affect.
Mid
Name the entity states. How do persist and merge differ?
Transient, managed, detached, removed. persist makes the object itself managed; merge copies it onto a managed instance and returns that.
Transient is new and not managed; managed lives in the persistence context and its changes are written automatically; detached means the context has closed; removed is scheduled for deletion. After merge, the argument stays detached.
What is dirty checking? Why does changing an entity produce an UPDATE without calling save?
Hibernate compares each managed entity with the snapshot taken at load time and issues UPDATEs for changed ones on flush.
Flushes run before commit or before a related query, so a managed entity inside a transaction doesn’t need save. The flip side: modifying an entity just for display also gets persisted.
When does LazyInitializationException happen and how do you fix it?
When you access a lazy association after the persistence context has closed; fix it by fetching what you need in the service and returning DTOs.
It usually happens in the controller or while Jackson serializes an entity. Fetch with a fetch join, @EntityGraph or a DTO projection inside the service’s transaction. Don’t “fix” it with EAGER or open-in-view; that hides the problem and causes N+1.
Senior
What is N+1, and how do you detect and fix it?
One query loads N parent rows, then each row triggers a lazy query: N+1 round trips. Fix it per use case.
Detect it with SQL logging, Hibernate statistics or tests that assert the statement count. Fix: fetch join or @EntityGraph for to-one associations, batch fetching (default_batch_fetch_size) as a safety net, DTO projections for read-only screens. For collections with pagination, query a page of ids first and then fetch by id, to avoid in-memory paging.
A JPA batch job processing a million rows runs out of memory and gets slower over time. Why, and what do you do?
The persistence context keeps every loaded entity plus its snapshot, so the heap grows linearly and every flush dirty-checks everything.
Fix:
- Process in chunks (read with keyset pagination),
flush()+clear()after each chunk. - Enable
hibernate.jdbc.batch_sizewithorder_inserts/order_updates, and use SEQUENCE ids so inserts can batch. - Use
readOnlyor aStatelessSessionwhen you don’t need dirty checking, or bulk UPDATEs / plain JDBC for mass operations.
Useful phrases for interviews
- “I default every association to LAZY and decide what to fetch per use case.”
- “That endpoint had a classic N+1: one query for the courses and one more per course for the teacher.”
- “Because the entity is managed, dirty checking picks up the change on flush, so there’s no need to call save.”
- “We use a DTO projection for read-only screens; it skips the persistence context entirely.”
- “Turning off open-in-view surfaced every place where we were lazily loading outside a transaction.”
- “Flush isn’t commit: the SQL has been sent, but nobody else sees it until the transaction commits.”
- “The second lookup didn’t hit the database; it came straight from the first-level cache.”
- “We keep a query-count assertion on our list endpoints, so an N+1 fails the build instead of reaching production.”
Next up: ORM and JPA part 5: JPQL, native queries, @Transactional and locking (open-session-in-view is covered there).
Notes based on the Fundamentals of Backend course (Ronin Engineer), rewritten in my own words.