hibernate/hibernate-orm · error · DetachedObjectException

Given proxy is not associated with the persistence context

Error message

Given proxy is not associated with the persistence context

What it means

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

Source

Thrown at hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java:1829

//		checkTransactionSynchStatus();

		//noinspection ConstantValue
		if ( object == null ) {
			throw new IllegalArgumentException( "Entity may not be null" );
		}

		final var lazyInitializer = extractLazyInitializer( object );
		if ( lazyInitializer != null ) {
			checkOwnsProxy( lazyInitializer );
			object = lazyInitializer.getImplementation();
		}

		return getEntityEntry( object ).getPersister().getEntityName();
	}

	private void checkOwnsProxy(@Nonnull LazyInitializer lazyInitializer) {
		if ( lazyInitializer.getSession() != this ) {
			throw new DetachedObjectException( "Given proxy is not associated with the persistence context" );
		}
	}

	@Nonnull
	private EntityEntry getEntityEntry(@Nonnull Object object) {
		final var entry = persistenceContext.getEntry( object );
		if ( entry == null ) {
			throw new UnmanagedObjectException( "Given entity is not associated with the persistence context" );
		}
		return entry;
	}

	@Override
	public String guessEntityName(@Nonnull Object object) {
		checkOpenOrWaitingForAutoClose();
		return getEntityNameResolver().resolveEntityName( object );
	}

View on GitHub (pinned to fad1729dce)

Solutions

  1. Only pass proxies obtained from this same Session; reload the entity with the current session if it was loaded elsewhere.
  2. Attach the proxy first (e.g. merge) before performing the operation on it.

When it happens

Trigger: Thrown at hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java:1829 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/e5846e03ece284b5. Report an issue: GitHub.