The proxy needs an open session to load. Touching it in the view layer is too late.
@Transactional(readOnly = true)
public Order load(Long id) {
return repo.findById(id).orElseThrow(); // session closes at return
}
// Later, in the controller:
order.getItems().size(); // BOOM
org.hibernate.LazyInitializationException:
failed to lazily initialize a collection of role: Order.items -
no Session
Fix: fetch what you need inside the transaction, or map to a DTO there.
Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.
Published 2026-08-11