diff --git a/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/spi/interceptor/LazyAttributeLoadingInterceptor.java b/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/spi/interceptor/LazyAttributeLoadingInterceptor.java index 47b6c3e3af..766807483d 100644 --- a/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/spi/interceptor/LazyAttributeLoadingInterceptor.java +++ b/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/spi/interceptor/LazyAttributeLoadingInterceptor.java @@ -46,7 +46,6 @@ public class LazyAttributeLoadingInterceptor private boolean allowLoadOutsideTransaction; private String sessionFactoryUuid; - public LazyAttributeLoadingInterceptor( String entityName, Set lazyFields, @@ -59,12 +58,21 @@ public class LazyAttributeLoadingInterceptor protected final Object intercept(Object target, String attributeName, Object value) { if ( !isAttributeLoaded( attributeName ) ) { - return loadAttribute( target, attributeName ); + Object loadedValue = fetchAttribute( target, attributeName ); + attributeInitialized( attributeName ); + return loadedValue; } return value; } - private Object loadAttribute(final Object target, final String attributeName) { + /** + * Fetches the lazy attribute. The attribute does not get associated with the entity. (To be used by hibernate methods) + */ + public Object fetchAttribute(final Object target, final String attributeName) { + return loadAttribute( target, attributeName ); + } + + protected Object loadAttribute(final Object target, final String attributeName) { return new Helper( this ).performWork( new LazyInitializationWork() { @Override @@ -173,6 +181,7 @@ public class LazyAttributeLoadingInterceptor CollectionTracker tracker = ( (SelfDirtinessTracker) target ).$$_hibernate_getCollectionTracker(); if ( tracker == null ) { ( (SelfDirtinessTracker) target ).$$_hibernate_clearDirtyAttributes(); + tracker = ( (SelfDirtinessTracker) target ).$$_hibernate_getCollectionTracker(); } tracker.add( fieldName, ( (Collection) value ).size() ); } 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 4f9d1d7347..ef8de17005 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 @@ -12,6 +12,7 @@ import java.util.HashSet; import java.util.Iterator; import org.hibernate.HibernateException; +import org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeLoadingInterceptor; import org.hibernate.collection.spi.PersistentCollection; import org.hibernate.engine.spi.CascadeStyle; import org.hibernate.engine.spi.CascadingAction; @@ -87,19 +88,26 @@ public final class Cascade { for ( int i = 0; i < types.length; i++) { final CascadeStyle style = cascadeStyles[i]; final String propertyName = propertyNames[i]; - if ( hasUninitializedLazyProperties && persister.getPropertyLaziness()[i] && ! action.performOnLazyProperty() ) { - //do nothing to avoid a lazy property initialization - continue; - } if ( style.doCascade( action ) ) { + Object child; + + // For bytecode enhanced entities, need to fetch the attribute + if ( hasUninitializedLazyProperties && persister.getPropertyLaziness()[i] && action.performOnLazyProperty() ) { + LazyAttributeLoadingInterceptor interceptor = persister.getInstrumentationMetadata().extractInterceptor( parent ); + child = interceptor.fetchAttribute( parent, propertyName ); + } + else { + child = persister.getPropertyValue( parent, i ); + } + cascadeProperty( action, cascadePoint, eventSource, componentPathStackDepth, parent, - persister.getPropertyValue( parent, i ), + child, types[i], style, propertyName,