code cleanups to EnhancementAsProxyLazinessInterceptor

Signed-off-by: Gavin King <gavin@hibernate.org>
This commit is contained in:
Gavin King 2024-04-21 16:02:51 +02:00
parent caab8a97fc
commit c71967d6c1
1 changed files with 91 additions and 93 deletions

View File

@ -11,12 +11,9 @@ import java.util.HashSet;
import java.util.Set; import java.util.Set;
import org.hibernate.HibernateException; import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.bytecode.BytecodeLogging; import org.hibernate.bytecode.BytecodeLogging;
import org.hibernate.bytecode.enhance.spi.LazyPropertyInitializer;
import org.hibernate.engine.spi.EntityKey; import org.hibernate.engine.spi.EntityKey;
import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.internal.util.collections.ArrayHelper;
import org.hibernate.metamodel.mapping.AttributeMapping; import org.hibernate.metamodel.mapping.AttributeMapping;
import org.hibernate.persister.entity.EntityPersister; import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.type.CompositeType; import org.hibernate.type.CompositeType;
@ -63,8 +60,8 @@ public class EnhancementAsProxyLazinessInterceptor extends AbstractLazyLoadInter
session.getFactory().getMappingMetamodel() session.getFactory().getMappingMetamodel()
.getEntityDescriptor( entityName ); .getEntityDescriptor( entityName );
if ( entityPersister.hasCollections() ) { if ( entityPersister.hasCollections() ) {
Type[] propertyTypes = entityPersister.getPropertyTypes(); final Type[] propertyTypes = entityPersister.getPropertyTypes();
String[] propertyNames = entityPersister.getPropertyNames(); final String[] propertyNames = entityPersister.getPropertyNames();
collectionAttributeNames = new HashSet<>(); collectionAttributeNames = new HashSet<>();
for ( int i = 0; i < propertyTypes.length; i++ ) { for ( int i = 0; i < propertyTypes.length; i++ ) {
Type propertyType = propertyTypes[i]; Type propertyType = propertyTypes[i];
@ -106,71 +103,69 @@ public class EnhancementAsProxyLazinessInterceptor extends AbstractLazyLoadInter
// Use `performWork` to group together multiple Session accesses // Use `performWork` to group together multiple Session accesses
return EnhancementHelper.performWork( return EnhancementHelper.performWork(
this, this,
(session, isTempSession) -> { (session, isTempSession) -> read( target, attributeName, session, isTempSession ),
final Object[] writtenAttributeValues;
final AttributeMapping[] writtenAttributeMappings;
final EntityPersister entityPersister =
session.getFactory().getMappingMetamodel()
.getEntityDescriptor( getEntityName() );
if ( writtenFieldNames != null && !writtenFieldNames.isEmpty() ) {
// enhancement has dirty-tracking available and at least one attribute was explicitly set
if ( writtenFieldNames.contains( attributeName ) ) {
// the requested attribute was one of the attributes explicitly set,
// we can just return the explicitly-set value
return entityPersister.getPropertyValue( target, attributeName );
}
// otherwise we want to save all the explicitly-set values in anticipation of
// the force initialization below so that we can "replay" them after the
// initialization
writtenAttributeValues = new Object[writtenFieldNames.size()];
writtenAttributeMappings = new AttributeMapping[writtenFieldNames.size()];
int index = 0;
for ( String writtenFieldName : writtenFieldNames ) {
writtenAttributeMappings[index] = entityPersister.findAttributeMapping( writtenFieldName );
writtenAttributeValues[index] = writtenAttributeMappings[index].getValue( target );
index++;
}
}
else {
writtenAttributeValues = null;
writtenAttributeMappings = null;
}
final Object initializedValue = forceInitialize(
target,
attributeName,
session,
isTempSession
);
setInitialized();
if ( writtenAttributeValues != null ) {
// here is the replaying of the explicitly set values we prepared above
for ( int i = 0; i < writtenAttributeMappings.length; i++ ) {
final AttributeMapping attribute = writtenAttributeMappings[i];
attribute.setValue( target, writtenAttributeValues[i] );
if ( inLineDirtyChecking ) {
asSelfDirtinessTracker( target ).$$_hibernate_trackChange( attribute.getAttributeName() );
}
}
writtenFieldNames.clear();
}
return initializedValue;
},
getEntityName(), getEntityName(),
attributeName attributeName
); );
} }
private Object read(
Object target, String attributeName, SharedSessionContractImplementor session, Boolean isTempSession) {
final Object[] writtenAttributeValues;
final AttributeMapping[] writtenAttributeMappings;
final EntityPersister entityPersister =
session.getFactory().getMappingMetamodel()
.getEntityDescriptor( getEntityName() );
if ( writtenFieldNames != null && !writtenFieldNames.isEmpty() ) {
// enhancement has dirty-tracking available and at least one attribute was explicitly set
if ( writtenFieldNames.contains( attributeName ) ) {
// the requested attribute was one of the attributes explicitly set,
// we can just return the explicitly-set value
return entityPersister.getPropertyValue( target, attributeName );
}
// otherwise we want to save all the explicitly-set values in anticipation of
// the force initialization below so that we can "replay" them after the
// initialization
writtenAttributeValues = new Object[writtenFieldNames.size()];
writtenAttributeMappings = new AttributeMapping[writtenFieldNames.size()];
int index = 0;
for ( String writtenFieldName : writtenFieldNames ) {
writtenAttributeMappings[index] = entityPersister.findAttributeMapping( writtenFieldName );
writtenAttributeValues[index] = writtenAttributeMappings[index].getValue(target);
index++;
}
}
else {
writtenAttributeValues = null;
writtenAttributeMappings = null;
}
final Object initializedValue = forceInitialize( target, attributeName, session, isTempSession );
setInitialized();
if ( writtenAttributeValues != null ) {
// here is the replaying of the explicitly set values we prepared above
for ( int i = 0; i < writtenAttributeMappings.length; i++ ) {
final AttributeMapping attribute = writtenAttributeMappings[i];
attribute.setValue(target, writtenAttributeValues[i] );
if ( inLineDirtyChecking ) {
asSelfDirtinessTracker(target).$$_hibernate_trackChange( attribute.getAttributeName() );
}
}
writtenFieldNames.clear();
}
return initializedValue;
}
private Object extractIdValue(Object target, String attributeName) { private Object extractIdValue(Object target, String attributeName) {
// access to the id or part of it for non-aggregated cid // access to the id or part of it for non-aggregated cid
if ( nonAggregatedCidMapper == null ) { if ( nonAggregatedCidMapper == null ) {
@ -186,12 +181,14 @@ public class EnhancementAsProxyLazinessInterceptor extends AbstractLazyLoadInter
} }
public Object forceInitialize(Object target, String attributeName) { public Object forceInitialize(Object target, String attributeName) {
BytecodeLogging.LOGGER.tracef( if ( BytecodeLogging.LOGGER.isTraceEnabled() ) {
"EnhancementAsProxyLazinessInterceptor#forceInitialize : %s#%s -> %s )", BytecodeLogging.LOGGER.tracef(
entityKey.getEntityName(), "EnhancementAsProxyLazinessInterceptor#forceInitialize : %s#%s -> %s )",
entityKey.getIdentifier(), entityKey.getEntityName(),
attributeName entityKey.getIdentifier(),
); attributeName
);
}
return EnhancementHelper.performWork( return EnhancementHelper.performWork(
this, this,
@ -201,29 +198,31 @@ public class EnhancementAsProxyLazinessInterceptor extends AbstractLazyLoadInter
); );
} }
public Object forceInitialize(Object target, String attributeName, SharedSessionContractImplementor session, boolean isTemporarySession) { public Object forceInitialize(
BytecodeLogging.LOGGER.tracef( Object target,
"EnhancementAsProxyLazinessInterceptor#forceInitialize : %s#%s -> %s )", String attributeName,
entityKey.getEntityName(), SharedSessionContractImplementor session,
entityKey.getIdentifier(), boolean isTemporarySession) {
attributeName if ( BytecodeLogging.LOGGER.isTraceEnabled() ) {
); BytecodeLogging.LOGGER.tracef(
"EnhancementAsProxyLazinessInterceptor#forceInitialize : %s#%s -> %s )",
entityKey.getEntityName(),
entityKey.getIdentifier(),
attributeName
);
}
final EntityPersister persister = session.getFactory() final EntityPersister persister =
.getRuntimeMetamodels() session.getFactory().getMappingMetamodel()
.getMappingMetamodel() .getEntityDescriptor( getEntityName() );
.getEntityDescriptor( getEntityName() );
if ( isTemporarySession ) { if ( isTemporarySession ) {
// Add an entry for this entity in the PC of the temp Session // Add an entry for this entity in the PC of the temp Session
session.getPersistenceContext().addEnhancedProxy( entityKey, asPersistentAttributeInterceptable( target ) ); session.getPersistenceContext()
.addEnhancedProxy( entityKey, asPersistentAttributeInterceptable( target ) );
} }
return persister.initializeEnhancedEntityUsedAsProxy( return persister.initializeEnhancedEntityUsedAsProxy( target, attributeName, session );
target,
attributeName,
session
);
} }
@Override @Override
@ -249,9 +248,8 @@ public class EnhancementAsProxyLazinessInterceptor extends AbstractLazyLoadInter
} }
if ( changed ) { if ( changed ) {
throw new HibernateException( throw new HibernateException( "identifier of an instance of " + entityKey.getEntityName()
"identifier of an instance of " + entityKey.getEntityName() + " was altered from " + oldValue + " to " + newValue + " was altered from " + oldValue + " to " + newValue );
);
} }
// otherwise, setId has been called but passing in the same value - just pass it through // otherwise, setId has been called but passing in the same value - just pass it through
@ -259,7 +257,7 @@ public class EnhancementAsProxyLazinessInterceptor extends AbstractLazyLoadInter
} }
if ( initializeBeforeWrite if ( initializeBeforeWrite
|| ( collectionAttributeNames != null && collectionAttributeNames.contains( attributeName ) ) ) { || collectionAttributeNames != null && collectionAttributeNames.contains( attributeName ) ) {
// we need to force-initialize the proxy - the fetch group to which the `attributeName` belongs // we need to force-initialize the proxy - the fetch group to which the `attributeName` belongs
try { try {
forceInitialize( target, attributeName ); forceInitialize( target, attributeName );
@ -340,7 +338,7 @@ public class EnhancementAsProxyLazinessInterceptor extends AbstractLazyLoadInter
} }
public boolean hasWrittenFieldNames() { public boolean hasWrittenFieldNames() {
return writtenFieldNames != null && writtenFieldNames.size() != 0; return writtenFieldNames != null && !writtenFieldNames.isEmpty();
} }
private enum Status { private enum Status {