Home โบ Java exceptions โบ LazyInitializationException
LazyInitializationException: causes and fixes
A lazily loaded association or collection was accessed after the Hibernate session had closed.
What it looks like
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.acme.Order.items, could not initialize proxy - no SessionExtends HibernateException in org.hibernate.
What it means
Associations such as @OneToMany are loaded lazily by default: Hibernate puts a proxy in the field and runs the query the first time you touch it. That only works while the session (the persistence context) is open. Once the transaction ends, and the entity is detached, touching the proxy fails with this exception. It typically appears in the controller or in JSON serialisation, after the service method returned.
Common causes
- Returning an entity from a transactional service and accessing a lazy collection in the controller or in Jackson.
- Spring Boot's open-in-view being disabled (or never relevant for async or batch code).
- Accessing a lazy field from another thread or after the transaction was committed.
- Serialising an entity directly instead of a DTO.
How to fix it
- Fetch what you need inside the transaction: a
JOIN FETCHquery, an@EntityGraph, orHibernate.initialize. - Map entities to DTOs inside the service and return the DTO.
- Avoid switching associations to EAGER globally: it causes N+1 queries and large joins.
- Keep
spring.jpa.open-in-view=falseand fix the access pattern, so database work stays in the service layer.
Broken and fixed
@Transactional
public Order get(Long id) { return repo.findById(id).orElseThrow(); }
// controller: order.getItems() -> no Session@Query("select o from Order o join fetch o.items where o.id = :id")
Optional<Order> findWithItems(@Param("id") Long id);๐ Unravel the failing code ๐ Unravel the fix
In Spring and enterprise code
Spring Boot enables open-in-view by default, which hides this error by keeping the session open during the web request but holds a database connection for the whole request. Many teams turn it off.
Debug it faster
Paste the whole stack trace into the stack trace beautifier to hide framework frames, jump to the root cause and find the first line of your own code. It runs in your browser, so the trace never leaves your machine.
Frequently asked questions
Is LazyInitializationException a checked or unchecked exception?
LazyInitializationException is an unchecked exception (it extends HibernateException, which is a RuntimeException), so you are not forced to catch it. Prevent it with validation, or handle it where the input enters the system.
Which package is LazyInitializationException in?
It lives in org.hibernate, so the import is import org.hibernate.LazyInitializationException;.
What is the first thing to check when I see LazyInitializationException?
Read the message and the first stack trace line in your own code. The usual causes are: Returning an entity from a transactional service and accessing a lazy collection in the controller or in Jackson; and Spring Boot's open-in-view being disabled (or never relevant for async or batch code).