Software⏱️ 2 min read📅 2026-06-03

How to Fix: How to fix org.hibernate.LazyInitializationException - could not initialize proxy - no Session

LazyInitializationException occurs when you try to access a lazy-loaded entity outside of the session context.

Quick Answer: To fix, either join the entity with its associated collection within the session or use eager loading when retrieving entities.

The Hibernate LazyInitializationException is thrown when the application attempts to initialize a proxy object outside of the context of an open Session. This exception can occur due to various reasons, including lazy loading or using a detached entity manager.

🛑 Root Causes of the Error

  • Lazy loading of entities outside of a Session.
  • Using a detached entity manager.

🔧 Proven Troubleshooting Steps

Method 1: Using a Session

  1. Step 1: Create a new Session and initialize the entity manager.
  2. Step 2: Use the entity manager to access the entity, instead of creating a detached instance.

Method 2: Enabling Eager Loading

  1. Step 1: Add the @Fetch(FetchMode.EAGER) annotation to the lazy-loaded field.
  2. Step 2: Use the @Transactional annotation with the Flush value set to false to prevent flushing of changes.

💡 Conclusion

To resolve the Hibernate LazyInitializationException, it is recommended to use a Session or enable eager loading. By following these methods, you can avoid this exception and ensure proper entity management.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions