diff --git a/hibernate-core/src/main/java/org/hibernate/engine/internal/Cascade.java b/hibernate-core/src/main/java/org/hibernate/engine/internal/Cascade.java index 7d20000b4d..badba7a9cf 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/internal/Cascade.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/internal/Cascade.java @@ -87,11 +87,16 @@ public final class Cascade { LOG.tracev( "Processing cascade {0} for: {1}", action, persister.getEntityName() ); } final PersistenceContext persistenceContext = eventSource.getPersistenceContextInternal(); - + final EntityEntry entry = persistenceContext.getEntry( parent ); + if ( entry != null && entry.getLoadedState() == null && entry.getStatus() == Status.MANAGED && persister.getBytecodeEnhancementMetadata() + .isEnhancedForLazyLoading() ) { + return; + } final Type[] types = persister.getPropertyTypes(); final String[] propertyNames = persister.getPropertyNames(); final CascadeStyle[] cascadeStyles = persister.getPropertyCascadeStyles(); final boolean hasUninitializedLazyProperties = persister.hasUninitializedLazyProperties( parent ); + for ( int i = 0; i < types.length; i++) { final CascadeStyle style = cascadeStyles[ i ]; final String propertyName = propertyNames[ i ]; @@ -109,7 +114,7 @@ public final class Cascade { // If parent is a detached entity being merged, // then parent will not be in the PersistenceContext // (so lazy attributes must not be initialized). - if ( persistenceContext.getEntry( parent ) == null ) { + if ( entry == null ) { // parent was not in the PersistenceContext continue; } diff --git a/hibernate-core/src/main/java/org/hibernate/event/internal/AbstractFlushingEventListener.java b/hibernate-core/src/main/java/org/hibernate/event/internal/AbstractFlushingEventListener.java index 086510ce1c..5752b8f463 100644 --- a/hibernate-core/src/main/java/org/hibernate/event/internal/AbstractFlushingEventListener.java +++ b/hibernate-core/src/main/java/org/hibernate/event/internal/AbstractFlushingEventListener.java @@ -139,9 +139,8 @@ public abstract class AbstractFlushingEventListener implements JpaBootstrapSensi //safe from concurrent modification because of how concurrentEntries() is implemented on IdentityMap for ( Map.Entry me : persistenceContext.reentrantSafeEntityEntries() ) { // for ( Map.Entry me : IdentityMap.concurrentEntries( persistenceContext.getEntityEntries() ) ) { - EntityEntry entry = me.getValue(); - Status status = entry.getStatus(); - if ( status == Status.MANAGED || status == Status.SAVING || status == Status.READ_ONLY ) { + final EntityEntry entry = me.getValue(); + if ( flushable( entry ) ) { cascadeOnFlush( session, entry.getPersister(), me.getKey(), context ); } } @@ -149,9 +148,9 @@ public abstract class AbstractFlushingEventListener implements JpaBootstrapSensi private static boolean flushable(EntityEntry entry) { final Status status = entry.getStatus(); - return ( status == Status.MANAGED && entry.getLoadedState() != null ) - || status == Status.SAVING - || status == Status.READ_ONLY; + return status == Status.MANAGED + || status == Status.SAVING + || status == Status.READ_ONLY; } private void cascadeOnFlush(EventSource session, EntityPersister persister, Object object, PersistContext anything)