HHH-10252 - Trigger the lazy loading of cascading attributes

This commit is contained in:
barreiro 2015-12-09 07:53:19 +00:00 committed by Steve Ebersole
parent 601bdaa678
commit 268a1eaa22
2 changed files with 25 additions and 8 deletions

View File

@ -46,7 +46,6 @@ public class LazyAttributeLoadingInterceptor
private boolean allowLoadOutsideTransaction; private boolean allowLoadOutsideTransaction;
private String sessionFactoryUuid; private String sessionFactoryUuid;
public LazyAttributeLoadingInterceptor( public LazyAttributeLoadingInterceptor(
String entityName, String entityName,
Set<String> lazyFields, Set<String> lazyFields,
@ -59,12 +58,21 @@ public LazyAttributeLoadingInterceptor(
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 );
attributeInitialized( 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
@ -173,6 +181,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,6 +12,7 @@
import java.util.Iterator; import java.util.Iterator;
import org.hibernate.HibernateException; import org.hibernate.HibernateException;
import org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeLoadingInterceptor;
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;
@ -87,19 +88,26 @@ 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
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( cascadeProperty(
action, action,
cascadePoint, cascadePoint,
eventSource, eventSource,
componentPathStackDepth, componentPathStackDepth,
parent, parent,
persister.getPropertyValue( parent, i ), child,
types[i], types[i],
style, style,
propertyName, propertyName,