hibernate/hibernate-orm · error · UnmanagedObjectException

Given entity is not associated with the persistence context

Error message

Given entity is not associated with the persistence context

What it means

Error "Given entity is not associated with the persistence context" thrown in hibernate/hibernate-orm.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/engine/internal/StatefulPersistenceContext.java:1704

	private void clearNullProperties() {
		nullAssociations = null;
	}

	@Override
	public boolean isReadOnly(Object entityOrProxy) {
		if ( entityOrProxy == null ) {
			throw new IllegalArgumentException( "Entity may not be null" );
		}

		final var lazyInitializer = extractLazyInitializer( entityOrProxy );
		if ( lazyInitializer != null ) {
			return lazyInitializer.isReadOnly();
		}
		else {
			final var ee = getEntry( entityOrProxy );
			if ( ee == null ) {
				throw new UnmanagedObjectException( "Given entity is not associated with the persistence context" );
			}
			return ee.isReadOnly();
		}
	}

	@Override
	public void setReadOnly(Object object, boolean readOnly) {
		if ( object == null ) {
			throw new IllegalArgumentException( "Entity may not be null" );
		}
		if ( isReadOnly( object ) != readOnly ) {
			final var lazyInitializer = extractLazyInitializer( object );
			if ( lazyInitializer != null ) {
				setProxyReadOnly( lazyInitializer, readOnly );
				if ( !lazyInitializer.isUninitialized() ) {
					setEntityReadOnly( lazyInitializer.getImplementation(), readOnly );
				}
			}

View on GitHub (pinned to fad1729dce)

Solutions

  1. Load or attach the entity to the current persistence context before the operation
  2. Check session.contains(entity) before invoking context-bound operations

When it happens

Trigger: Thrown at hibernate-core/src/main/java/org/hibernate/engine/internal/StatefulPersistenceContext.java:1704 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/671730020ba5396d. Report an issue: GitHub.