LazyInitializationException ☕ Java

org.hibernate.LazyInitializationException: could not initialize proxy - no Session

A lazy-loaded association was accessed after the Hibernate session/transaction closed.

Seen on: Java

Meaning

Lazy collections and proxies load on first access, which needs an open session. Accessing them in the controller/view or during JSON serialization after the transaction ended fails.

Common causes

  • Accessing lazy relations outside @Transactional
  • Serializing entities to JSON after the service returns
  • Open Session in View disabled

⚡ Quick fix

  1. Fetch what you need in the query (JOIN FETCH / @EntityGraph)
  2. Map to DTOs inside the transaction
  3. Mark the service method @Transactional(readOnly = true)

Detailed fix by platform

Java

  1. @Query("select o from Order o join fetch o.items where o.id = :id")

How to diagnose

  1. Association — Which relation?
  2. Boundary — Where does the transaction end?

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.