LazyInitializationException — Causes and Fixes
Fast recall
Quick revision
Core answer
EntityManager to execute its SELECT; once the session is gone (transaction committed, entity detached), the proxy has no way to load the data. Wrong fixes: `FetchType.EAGER` (over-fetching, Cartesian products) and `spring.jpa.open-in-view = true` (Spring Boot's default — silent N+1 in Jackson serialization). Right fixes: `JOIN FETCH` / `@EntityGraph` in the repository so the association loads in the transaction; DTO projections (select new OrderDto(...)) that bypass entities; `Hibernate.initialize(entity.getItems())` as a targeted workaround inside the transaction; `@Transactional` on the calling method to extend the boundary (not a general fix). Always `spring.jpa.open-in-view = false` in production and return DTOs from controllers, not entities.Deep understanding of Hibernate session lifecycle and why common quick-fixes (EAGER, OSIV) are anti-patterns
Setting the association to EAGER globally to silence the exception — over-fetching across all queries; or enabling open-session-in-view that masks the problem with hidden queries
Explain why OSIV is an anti-pattern in REST APIs, describe `Hibernate.initialize()` as a targeted workaround, and distinguish session-scoped vs transaction-scoped persistence contexts
Interview-ready guide
Interview answer
LazyInitializationException is Hibernate telling you that you tried to load data after the database session was closed. It is one of the most common JPA bugs and always has the same root cause.
Why it happens:
order.getItems()is a Hibernate proxy — needs an openEntityManagerto execute the SELECT- The
EntityManagercloses at the end of the@Transactionalmethod - Accessing
order.getItems()in the controller fires the proxy, which has no session
`open-in-view` — why not to rely on it:
- Default is true: Spring keeps the
EntityManageropen for the entire HTTP request - Hides the bug in development; in production, each lazy load is an extra query during serialization — N+1 in disguise
- Set
spring.jpa.open-in-view=falseeverywhere
The three fixes:
- JOIN FETCH or
@EntityGraph— fetch in the original query - DTO projection —
select new OrderDto(...); no proxies, no error possible Hibernate.initialize()— call inside@Transactionalto force-load the proxy before close
The architectural rule:
- Disable OSIV
- Return DTOs from controllers
- Map entities -> DTOs inside
@Transactionalservice methods - Add a query-count integration test for every endpoint
LazyInitializationException — Causes and Fixes
Direct Answer
LazyInitializationException means you accessed a lazy-loaded association after the persistence context closed. The proxy needs an open EntityManager to execute its SELECT; once the session is gone (transaction committed, entity detached), the proxy has no way to load the data. Wrong fixes: FetchType.EAGER (over-fetching, Cartesian products) and spring.jpa.open-in-view = true (Spring Boot's default — silent N+1 in Jackson serialization). Right fixes: JOIN FETCH / @EntityGraph in the repository so the association loads in the transaction; DTO projections (select new OrderDto(...)) that bypass entities; Hibernate.initialize(entity.getItems()) as a targeted workaround inside the transaction; @Transactional on the calling method to extend the boundary (not a general fix). Always spring.jpa.open-in-view = false in production and return DTOs from controllers, not entities.
Common Mistakes & Pitfalls
Setting the association to EAGER globally to silence the exception — over-fetching across all queries; or enabling open-session-in-view that masks the problem with hidden queries
Check your understanding
Answer these without looking back at the article.
- Why is Open Session In View considered an anti-pattern for REST APIs specifically?
- How does `Hibernate.initialize()` work internally — what SQL does it execute?
- If you serialize JPA entities directly to JSON with Jackson, what other problems besides `LazyInitializationException` can occur?
- How would you migrate a large codebase from OSIV-on to OSIV-off without breaking everything?
Related interview prep categories
2Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.