HHH-15218 @OptimisticLocking(DIRTY) leads to wrong query during delete of circular reference

This commit is contained in:
Andrea Boriero 2022-07-05 16:34:54 +02:00 committed by Sanne Grinovero
parent 0a013ed8a4
commit 06e44f2adb
1 changed files with 13 additions and 0 deletions

View File

@ -30,6 +30,7 @@ import org.hibernate.metamodel.mapping.NaturalIdMapping;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.stat.internal.StatsHelper;
import org.hibernate.stat.spi.StatisticsImplementor;
import org.hibernate.tuple.entity.EntityMetamodel;
import org.hibernate.type.TypeHelper;
/**
@ -201,6 +202,18 @@ public class EntityUpdateAction extends EntityAction {
entry.postUpdate( instance, state, nextVersion );
}
if ( entry.getStatus() == Status.DELETED ) {
final EntityMetamodel entityMetamodel = persister.getEntityMetamodel();
final boolean isImpliedOptimisticLocking = !entityMetamodel.isVersioned()
&& entityMetamodel.getOptimisticLockStyle().isAllOrDirty();
if ( isImpliedOptimisticLocking && entry.getLoadedState() != null ) {
// The entity will be deleted and because we are going to create a delete statement that uses
// all the state values in the where clause, the entry state needs to be updated otherwise the statement execution will
// not delete any row (see HHH-15218).
entry.postUpdate( instance, state, nextVersion );
}
}
final StatisticsImplementor statistics = factory.getStatistics();
if ( persister.canWriteToCache() ) {
if ( persister.isCacheInvalidationRequired() || entry.getStatus() != Status.MANAGED ) {