Skip to content

Commit cef03a3

Browse files
committed
HHH-18713 saveOrUpdate changed behaviour with bytecode enhancer
1 parent b26441a commit cef03a3

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

hibernate-core/src/main/java/org/hibernate/engine/internal/Cascade.java

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.util.Collection;
1111
import java.util.Iterator;
1212
import java.util.List;
13+
import java.util.Set;
1314

1415
import org.hibernate.HibernateException;
1516
import org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeLoadingInterceptor;
@@ -19,6 +20,7 @@
1920
import org.hibernate.engine.spi.CollectionEntry;
2021
import org.hibernate.engine.spi.EntityEntry;
2122
import org.hibernate.engine.spi.PersistenceContext;
23+
import org.hibernate.engine.spi.SelfDirtinessTracker;
2224
import org.hibernate.engine.spi.SessionFactoryImplementor;
2325
import org.hibernate.engine.spi.Status;
2426
import org.hibernate.event.spi.DeleteContext;
@@ -27,8 +29,6 @@
2729
import org.hibernate.internal.CoreMessageLogger;
2830
import org.hibernate.persister.collection.CollectionPersister;
2931
import org.hibernate.persister.entity.EntityPersister;
30-
import org.hibernate.pretty.MessageHelper;
31-
import org.hibernate.proxy.HibernateProxy;
3232
import org.hibernate.type.AnyType;
3333
import org.hibernate.type.AssociationType;
3434
import org.hibernate.type.CollectionType;
@@ -38,13 +38,13 @@
3838
import org.hibernate.type.ForeignKeyDirection;
3939
import org.hibernate.type.ManyToOneType;
4040
import org.hibernate.type.OneToOneType;
41-
import org.hibernate.type.OneToOneType;
4241
import org.hibernate.type.Type;
4342

43+
import static org.hibernate.engine.internal.ManagedTypeHelper.asManagedEntity;
44+
import static org.hibernate.engine.internal.ManagedTypeHelper.asSelfDirtinessTrackerOrNull;
4445
import static org.hibernate.engine.internal.ManagedTypeHelper.isHibernateProxy;
4546
import static org.hibernate.engine.spi.CascadingActions.CHECK_ON_FLUSH;
4647
import static org.hibernate.pretty.MessageHelper.infoString;
47-
import static org.hibernate.type.ForeignKeyDirection.TO_PARENT;
4848

4949
/**
5050
* Delegate responsible for, in conjunction with the various
@@ -97,15 +97,29 @@ public static <T> void cascade(
9797
final PersistenceContext persistenceContext = eventSource.getPersistenceContextInternal();
9898
final boolean enhancedForLazyLoading = persister.getBytecodeEnhancementMetadata().isEnhancedForLazyLoading();
9999
final EntityEntry entry;
100+
final Set<String> dirtyAttributes;
100101
if ( enhancedForLazyLoading ) {
101102
entry = persistenceContext.getEntry( parent );
102-
if ( entry != null
103-
&& entry.getLoadedState() == null
104-
&& entry.getStatus() == Status.MANAGED ) {
105-
return;
103+
if ( entry != null && entry.getLoadedState() == null && entry.getStatus() == Status.MANAGED ) {
104+
final SelfDirtinessTracker selfDirtinessTracker = asSelfDirtinessTrackerOrNull( parent );
105+
if ( selfDirtinessTracker == null ) {
106+
return;
107+
}
108+
else {
109+
if ( asManagedEntity( parent ).$$_hibernate_useTracker() ) {
110+
dirtyAttributes = Set.of( selfDirtinessTracker.$$_hibernate_getDirtyAttributes() );
111+
}
112+
else {
113+
dirtyAttributes = null;
114+
}
115+
}
116+
}
117+
else {
118+
dirtyAttributes = null;
106119
}
107120
}
108121
else {
122+
dirtyAttributes = null;
109123
entry = null;
110124
}
111125
final Type[] types = persister.getPropertyTypes();
@@ -114,8 +128,11 @@ public static <T> void cascade(
114128
final boolean hasUninitializedLazyProperties = persister.hasUninitializedLazyProperties( parent );
115129

116130
for ( int i = 0; i < types.length; i++) {
117-
final CascadeStyle style = cascadeStyles[ i ];
118131
final String propertyName = propertyNames[ i ];
132+
if ( dirtyAttributes != null && !dirtyAttributes.contains( propertyName ) ) {
133+
return;
134+
}
135+
final CascadeStyle style = cascadeStyles[ i ];
119136
final Type type = types[i];
120137
final boolean isUninitializedProperty =
121138
hasUninitializedLazyProperties &&

hibernate-core/src/main/java/org/hibernate/engine/internal/ManagedTypeHelper.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,15 @@ public static SelfDirtinessTracker asSelfDirtinessTracker(final Object entity) {
390390
throw new ClassCastException( "Object of type '" + entity.getClass() + "' can't be cast to SelfDirtinessTracker" );
391391
}
392392

393+
public static SelfDirtinessTracker asSelfDirtinessTrackerOrNull(final Object entity) {
394+
Objects.requireNonNull( entity );
395+
if ( entity instanceof PrimeAmongSecondarySupertypes ) {
396+
PrimeAmongSecondarySupertypes t = (PrimeAmongSecondarySupertypes) entity;
397+
return t.asSelfDirtinessTracker();
398+
}
399+
return null;
400+
}
401+
393402
/**
394403
* Cast the object to an HibernateProxy, or return null in case it is not an instance of HibernateProxy
395404
* @param entity the entity to cast

0 commit comments

Comments
 (0)