hibernate/hibernate-orm · error · HibernateException

Illegally attempted to associate proxy [{}#{}] with two open

Error message

Illegally attempted to associate proxy [{}#{}] with two open sessions

What it means

Error "Illegally attempted to associate proxy [{}#{}] with two open sessions" thrown in hibernate/hibernate-orm.

Source

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

		return !initialized;
	}

	@Override
	public final SharedSessionContractImplementor getSession() {
		return session;
	}

	@Override
	public final void setSession(SharedSessionContractImplementor session) throws HibernateException {
		if ( session != this.session ) {
			// check for session == null first, since it is less expensive
			if ( session == null ) {
				unsetSession();
			}
			else if ( isConnectedToSession() ) {
				//TODO: perhaps this should be some other RuntimeException...
				CORE_LOGGER.attemptToAssociateProxyWithTwoOpenSessions( entityName, id );
				throw new HibernateException( "Illegally attempted to associate proxy ["
						+ entityName + "#" + id + "] with two open sessions" );
			}
			else {
				// session != null
				this.session = session;
				if ( readOnlyBeforeAttachedToSession == null ) {
					// use the default read-only/modifiable setting
					setReadOnly( session.getPersistenceContext().isDefaultReadOnly()
							|| !getEntityDescriptor().isMutable() );
				}
				else {
					// use the read-only/modifiable setting indicated during deserialization
					setReadOnly( readOnlyBeforeAttachedToSession );
					readOnlyBeforeAttachedToSession = null;
				}
			}
		}
	}

View on GitHub (pinned to fad1729dce)

Solutions

  1. Associate the proxy with only one open Session; evict or close the first session before attaching to another.
  2. Use merge() to move state between sessions instead of reusing the proxy.

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/94e2f53c69f962d7. Report an issue: GitHub.