From 3ef251dfb2003c5f2e5440f0101f8d44c7b050c7 Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Wed, 6 Sep 2023 17:05:28 +0200 Subject: [PATCH] HHH-17049 Bytecode Enhancement, extra records created for associations created in constructor --- .../main/java/org/hibernate/engine/internal/Cascade.java | 9 +++++++-- .../event/internal/AbstractFlushingEventListener.java | 6 +++--- 2 files changed, 10 insertions(+), 5 deletions(-) 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 cd924d8839..dd0f3e22d0 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 @@ -86,11 +86,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 ]; @@ -108,7 +113,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 6f138da12a..451b5c74f4 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 @@ -148,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)