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
Proxy ownership guard in checkOwnsProxy: the lazy proxy was loaded by a different session than the one being used, so this session refuses to operate on it. Re-attach or load the proxy within the current session.
Solutions
- Only pass proxies obtained from this same Session; reload the entity with the current session if it was loaded elsewhere.
- Attach the proxy first (e.g. merge) before performing the operation on it.
Defensive patterns
Strategy: validation
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.
Appendix: 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)