hibernate/hibernate-orm · error · LazyInitializationException

Could not initialize proxy

Error message

Could not initialize proxy [{}#{}] - session was closed or disconnected

What it means

LazyInitializationException from permissiveInitialization's final else branch: allowLoadOutsideTransaction is enabled, the proxy is not attached to a usable session, and the previously tried session is neither open-and-waiting nor connected, so no fallback loading path remains. The faulty state is a detached proxy whose temp-session load is impossible; initialize before detaching.

Solutions

  1. Initialize the proxy while the session is open and connected.
  2. Catch LazyInitializationException and reload within an active session.
Defensive patterns

Strategy: try-catch

When it happens

Trigger: A lazy proxy is accessed outside the lifecycle of its owning Hibernate Session.

Common situations: LazyInitializationException-style failures when rendering entities after the transaction/Session closed, common in web views and serialization.


AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22). Data as JSON: /api/errors/e109fba45c9ff5cd. Report an issue: GitHub.

Appendix: source

Thrown at hibernate-core/src/main/java/org/hibernate/proxy/AbstractLazyInitializer.java:262

						session.close();
					}
					catch (Exception e) {
						CORE_LOGGER.unableToCLoseTempSession();
					}
				}
			}
			catch (Exception e) {
				throw new LazyInitializationException( "Could not initialize proxy ["
						+ entityName + "#" + id + "]: " + e.getMessage() );
			}
		}
		else if ( session.isOpenOrWaitingForAutoClose() && session.isConnected() ) {
			target = immediateLoad( session );
			initialized = true;
			checkTargetState( session );
		}
		else {
			throw new LazyInitializationException( "Could not initialize proxy ["
					+ entityName + "#" + id + "] - session was closed or disconnected" );
		}
	}

	private Object immediateLoad(SharedSessionContractImplementor session) {
		if ( temporalIdentifier != null ) {
			final var influencers = session.getLoadQueryInfluencers();
			final Object previous = influencers.getTemporalIdentifier();
			influencers.setTemporalIdentifier( temporalIdentifier );
			try {
				return session.immediateLoad( entityName, id );
			}
			finally {
				influencers.setTemporalIdentifier( previous );
			}
		}
		else {
			return session.immediateLoad( entityName, id );

View on GitHub (pinned to fad1729dce)