hibernate/hibernate-orm · error · HibernateException

entity is not associated with the session: {}

Error message

entity is not associated with the session: {}

What it means

Error "entity is not associated with the session: {}" thrown in hibernate/hibernate-orm.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java:1569

		return conjunction;
	}

	@Override
	public Object initializeLazyProperty(String fieldName, Object entity, SharedSessionContractImplementor session) {
		return hasCollections() && getPropertyTypes()[getPropertyIndex( fieldName )] instanceof CollectionType collectionType
				? initializedLazyCollection( fieldName, entity, collectionType, session )
				: initializedLazyField( fieldName, entity, session );
	}

	private Object initializedLazyField(
			String fieldName,
			Object entity,
			SharedSessionContractImplementor session) {
		final Object id = session.getContextEntityIdentifier( entity );
		final var entry = session.getPersistenceContext().getEntry( entity );
		if ( entry == null ) {
			throw new HibernateException( "entity is not associated with the session: " + id );
		}

		if ( CORE_LOGGER.isTraceEnabled() ) {
			CORE_LOGGER.initializingLazyPropertiesOf(
					infoString( this, id, getFactory() ),
					fieldName
			);
		}

		// attempt to read it from second-level cache
		if ( session.getCacheMode().isGetEnabled()
				&& isLazyPropertiesCacheable() ) {
			final Object cachedValue = initializeLazyFieldFromCache( fieldName, entity, session, id, entry );
			if ( cachedValue != UNFETCHED_PROPERTY ) {
				// The following should be redundant, since the setter should have set this already.
				// interceptor.attributeInitialized(fieldName);

				// NOTE EARLY EXIT!!!

View on GitHub (pinned to fad1729dce)

Solutions

  1. Re-attach the entity with merge() or load it in the current session before performing the operation.
  2. Do not pass detached entities to operations requiring a managed instance.

When it happens

Trigger: Thrown when an operation requires the entity to be managed by the current session but the instance is detached or transient.

Common situations: See trigger scenarios.


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