HHH-10252 - Trigger the lazy loading of cascading attributes (5.0)

This commit is contained in:
barreiro 2015-12-09 07:53:19 +00:00
parent 5e33aacd01
commit 7a20fed92b
2 changed files with 32 additions and 7 deletions

View File

@ -62,12 +62,21 @@ public LazyAttributeLoader(SessionImplementor session, Set<String> lazyFields, S
protected final Object intercept(Object target, String attributeName, Object value) { protected final Object intercept(Object target, String attributeName, Object value) {
if ( !isAttributeLoaded( attributeName ) ) { if ( !isAttributeLoaded( attributeName ) ) {
return loadAttribute( target, attributeName ); Object loadedValue = fetchAttribute( target, attributeName );
setLoaded( attributeName );
return loadedValue;
} }
return value; 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( return new Helper( this ).performWork(
new LazyInitializationWork() { new LazyInitializationWork() {
@Override @Override
@ -167,6 +176,7 @@ private void takeCollectionSizeSnapshot(Object target, String fieldName, Object
CollectionTracker tracker = ( (SelfDirtinessTracker) target ).$$_hibernate_getCollectionTracker(); CollectionTracker tracker = ( (SelfDirtinessTracker) target ).$$_hibernate_getCollectionTracker();
if ( tracker == null ) { if ( tracker == null ) {
( (SelfDirtinessTracker) target ).$$_hibernate_clearDirtyAttributes(); ( (SelfDirtinessTracker) target ).$$_hibernate_clearDirtyAttributes();
tracker = ( (SelfDirtinessTracker) target ).$$_hibernate_getCollectionTracker();
} }
tracker.add( fieldName, ( (Collection) value ).size() ); tracker.add( fieldName, ( (Collection) value ).size() );
} }

View File

@ -12,11 +12,13 @@
import java.util.Iterator; import java.util.Iterator;
import org.hibernate.HibernateException; import org.hibernate.HibernateException;
import org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeLoader;
import org.hibernate.collection.spi.PersistentCollection; import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.engine.spi.CascadeStyle; import org.hibernate.engine.spi.CascadeStyle;
import org.hibernate.engine.spi.CascadingAction; import org.hibernate.engine.spi.CascadingAction;
import org.hibernate.engine.spi.CollectionEntry; import org.hibernate.engine.spi.CollectionEntry;
import org.hibernate.engine.spi.EntityEntry; import org.hibernate.engine.spi.EntityEntry;
import org.hibernate.engine.spi.PersistentAttributeInterceptable;
import org.hibernate.engine.spi.Status; import org.hibernate.engine.spi.Status;
import org.hibernate.event.spi.EventSource; import org.hibernate.event.spi.EventSource;
import org.hibernate.internal.CoreLogging; import org.hibernate.internal.CoreLogging;
@ -87,19 +89,32 @@ public static void cascade(
for ( int i = 0; i < types.length; i++) { for ( int i = 0; i < types.length; i++) {
final CascadeStyle style = cascadeStyles[i]; final CascadeStyle style = cascadeStyles[i];
final String propertyName = propertyNames[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 ) ) { if ( style.doCascade( action ) ) {
Object child;
// For bytecode enhanced entities, need to fetch the attribute
boolean lazy = hasUninitializedLazyProperties && persister.getPropertyLaziness()[i] && action.performOnLazyProperty();
if ( lazy && parent instanceof PersistentAttributeInterceptable ) {
Object interceptor = ( (PersistentAttributeInterceptable) parent ).$$_hibernate_getInterceptor();
if ( interceptor instanceof LazyAttributeLoader ) {
child = ( (LazyAttributeLoader) interceptor ).fetchAttribute( parent, propertyName );
}
else {
child = null;
}
}
else {
child = persister.getPropertyValue( parent, i );
}
cascadeProperty( cascadeProperty(
action, action,
cascadePoint, cascadePoint,
eventSource, eventSource,
componentPathStackDepth, componentPathStackDepth,
parent, parent,
persister.getPropertyValue( parent, i ), child,
types[i], types[i],
style, style,
propertyName, propertyName,