HHH-10252 - Trigger the lazy loading of cascading attributes
This commit is contained in:
parent
601bdaa678
commit
268a1eaa22
|
@ -46,7 +46,6 @@ public class LazyAttributeLoadingInterceptor
|
|||
private boolean allowLoadOutsideTransaction;
|
||||
private String sessionFactoryUuid;
|
||||
|
||||
|
||||
public LazyAttributeLoadingInterceptor(
|
||||
String entityName,
|
||||
Set<String> 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() );
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in New Issue