Coding⏱️ 2 min read📅 2026-05-31

How to Fix: How to fix Hibernate LazyInitializationException: failed to lazily initialize a collection of roles, could not initialize proxy - no Session

Fix Hibernate LazyInitializationException by disabling lazy initialization or setting discriminator type to transient.

Quick Answer: Use @LazyInitializationEnabled(false) on your Hibernate session or set the property hibernate.discriminator_type to 'transient' in your application.properties file.

The Hibernate LazyInitializationException occurs when you try to access a lazy-loaded property outside of the session context. This happens because the proxy object is not initialized until it's within the session.

✅ Best Solutions to Fix It

Method 1: Eager Loading of the Collection

  1. Step 1: Add @Fetch(FetchMode.JOIN) annotation to the lazy-loaded property in your entity.

Method 2: Creating a Joiner

  1. Step 1: Create a joiner that fetches the user's roles and authorities when needed.

🎯 Final Words

To avoid this exception, you can use either eager loading of the collection or create a joiner to fetch the required data. Both methods are effective and should be used depending on your specific requirements.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions