HHH-16745 Consider loading entity entries when checking if transient

This commit is contained in:
Marco Belladelli 2023-06-07 14:58:41 +02:00
parent ab0f320cd2
commit 75adba3376
No known key found for this signature in database
GPG Key ID: D1D0C3030AE3AA35
1 changed files with 13 additions and 1 deletions

View File

@ -266,6 +266,7 @@ import org.hibernate.sql.results.graph.Fetch;
import org.hibernate.sql.results.graph.FetchParent;
import org.hibernate.sql.results.graph.Fetchable;
import org.hibernate.sql.results.graph.FetchableContainer;
import org.hibernate.sql.results.graph.entity.LoadingEntityEntry;
import org.hibernate.sql.results.graph.entity.internal.EntityResultImpl;
import org.hibernate.sql.results.graph.internal.ImmutableFetchList;
import org.hibernate.sql.results.internal.SqlSelectionImpl;
@ -3862,12 +3863,23 @@ public abstract class AbstractEntityPersister
}
// check the version unsaved-value, if appropriate
final Object version = getVersion( entity );
if ( isVersioned() ) {
// let this take precedence if defined, since it works for
// assigned identifiers
final Object version = getVersion( entity );
final Boolean result = versionMapping.getUnsavedStrategy().isUnsaved( version );
if ( result != null ) {
if ( result && version == null && session.getPersistenceContext().hasLoadContext() ) {
// check if we're currently loading this entity instance, the version
// will be null but the entity cannot be considered transient
final EntityKey entityKey = new EntityKey( id, this );
final LoadingEntityEntry loadingEntityEntry = session.getPersistenceContext()
.getLoadContexts()
.findLoadingEntityEntry( entityKey );
if ( loadingEntityEntry != null && loadingEntityEntry.getEntityInstance() == entity ) {
return false;
}
}
return result;
}
}