hibernate/hibernate-orm · error · UnsupportedOperationException

PersistenceContext not available for immutable entity

Error message

PersistenceContext not available for immutable entity

What it means

Error "PersistenceContext not available for immutable entity" thrown in hibernate/hibernate-orm.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/engine/internal/EntityEntryImpl.java:565

		}
	}

	@Override
	public <T extends EntityEntryExtraState> T getExtraState(Class<T> extraStateType) {
		if ( next == null ) {
			return null;
		}
		else if ( extraStateType.isInstance( next ) ) {
			return extraStateType.cast( next );
		}
		else {
			return next.getExtraState( extraStateType );
		}
	}

	public PersistenceContext getPersistenceContext() {
		if ( persistenceContext == null ) {
			throw new UnsupportedOperationException( "PersistenceContext not available for immutable entity" );
		}
		return persistenceContext;
	}

	/**
	 * Saves the value for the given enum property.
	 *
	 * @param state
	 *			identifies the value to store
	 * @param value
	 *			the value to store; The caller must make sure that it matches
	 *			the given identifier
	 */
	<E extends Enum<E>> void setCompressedValue(EnumState<E> state, E value) {
		// reset the bits for the given property to 0
		compressedState &= state.getUnsetMask();
		// store the numeric representation of the enum value at the right offset
		compressedState |= ( state.getValue( value ) << state.getOffset() );

View on GitHub (pinned to fad1729dce)

Solutions

  1. Do not request the persistence context entry for an @Immutable entity that was never loaded into it
  2. Load the entity through the session before accessing its entry

When it happens

Trigger: Thrown at hibernate-core/src/main/java/org/hibernate/engine/internal/EntityEntryImpl.java:565 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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