Skip to content

Commit 8863cda

Browse files
committed
refactor a method of AbstractEntityPersister
1 parent b52e18f commit 8863cda

File tree

1 file changed

+47
-36
lines changed

1 file changed

+47
-36
lines changed

hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java

Lines changed: 47 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@
281281
import static org.hibernate.internal.util.collections.CollectionHelper.toSmallList;
282282
import static org.hibernate.loader.ast.internal.MultiKeyLoadHelper.supportsSqlArrayType;
283283
import static org.hibernate.metamodel.RepresentationMode.POJO;
284+
import static org.hibernate.metamodel.mapping.EntityDiscriminatorMapping.DISCRIMINATOR_ROLE_NAME;
284285
import static org.hibernate.metamodel.mapping.internal.GeneratedValuesProcessor.getGeneratedAttributes;
285286
import static org.hibernate.metamodel.mapping.internal.MappingModelCreationHelper.buildBasicAttributeMapping;
286287
import static org.hibernate.metamodel.mapping.internal.MappingModelCreationHelper.buildEncapsulatedCompositeIdentifierMapping;
@@ -1766,7 +1767,7 @@ protected void initializeLazyProperty(Object entity, EntityEntry entry, Object p
17661767
setPropertyValue( entity, index, propValue );
17671768
final var loadedState = entry.getLoadedState();
17681769
if ( loadedState != null ) {
1769-
// object have been loaded with setReadOnly(true); HHH-2236
1770+
// object has been loaded with setReadOnly(true); HHH-2236
17701771
loadedState[index] = type.deepCopy( propValue, factory );
17711772
}
17721773
// If the entity has deleted state, then update that as well
@@ -2281,7 +2282,7 @@ private DiscriminatorType<?> buildDiscriminatorType() {
22812282
discriminatorBasicType,
22822283
new UnifiedAnyDiscriminatorConverter<>(
22832284
getNavigableRole()
2284-
.append( EntityDiscriminatorMapping.DISCRIMINATOR_ROLE_NAME ),
2285+
.append( DISCRIMINATOR_ROLE_NAME ),
22852286
factory.getTypeConfiguration().getJavaTypeRegistry()
22862287
.resolveDescriptor( discriminatedType() ),
22872288
discriminatorBasicType.getRelationalJavaType(),
@@ -3465,9 +3466,9 @@ public void addDiscriminatorToInsertGroup(MutationGroupBuilder insertGroupBuilde
34653466
public void addSoftDeleteToInsertGroup(MutationGroupBuilder insertGroupBuilder) {
34663467
if ( softDeleteMapping != null ) {
34673468
final TableInsertBuilder insertBuilder = insertGroupBuilder.getTableDetailsBuilder( getIdentifierTableName() );
3468-
final MutatingTableReference mutatingTable = insertBuilder.getMutatingTable();
3469-
final ColumnReference columnReference = new ColumnReference( mutatingTable, softDeleteMapping );
3470-
final ColumnValueBinding nonDeletedValueBinding = softDeleteMapping.createNonDeletedValueBinding( columnReference );
3469+
final var mutatingTable = insertBuilder.getMutatingTable();
3470+
final var columnReference = new ColumnReference( mutatingTable, softDeleteMapping );
3471+
final var nonDeletedValueBinding = softDeleteMapping.createNonDeletedValueBinding( columnReference );
34713472
insertBuilder.addValueColumn( nonDeletedValueBinding );
34723473
}
34733474
}
@@ -3512,7 +3513,7 @@ private Object doLoad(Object id, Object optionalObject, LockOptions lockOptions,
35123513
CORE_LOGGER.fetchingEntity( infoString( this, id, getFactory() ) );
35133514
}
35143515

3515-
final SingleIdEntityLoader<?> loader = determineLoaderToUse( session, lockOptions );
3516+
final var loader = determineLoaderToUse( session, lockOptions );
35163517
return optionalObject == null
35173518
? loader.load( id, lockOptions, readOnly, session )
35183519
: loader.load( id, optionalObject, lockOptions, readOnly, session );
@@ -3558,47 +3559,57 @@ public Object initializeEnhancedEntityUsedAsProxy(
35583559
Object entity,
35593560
String nameOfAttributeBeingAccessed,
35603561
SharedSessionContractImplementor session) {
3561-
final var enhancementMetadata = getBytecodeEnhancementMetadata();
3562-
if ( enhancementMetadata.extractLazyInterceptor( entity )
3562+
if ( getBytecodeEnhancementMetadata().extractLazyInterceptor( entity )
35633563
instanceof EnhancementAsProxyLazinessInterceptor proxyInterceptor ) {
3564-
35653564
final var entityKey = proxyInterceptor.getEntityKey();
3566-
final Object identifier = entityKey.getIdentifier();
3567-
3568-
Object loaded = null;
3569-
if ( canReadFromCache && session.isEventSource() ) {
3570-
final var eventSource = (EventSource) session;
3571-
loaded = eventSource.loadFromSecondLevelCache( this, entityKey, entity, LockMode.NONE );
3572-
}
3573-
if ( loaded == null ) {
3574-
final var lockOptions = new LockOptions();
3575-
loaded = determineLoaderToUse( session, lockOptions ).load( identifier, entity, lockOptions, session );
3576-
}
3577-
3565+
final Object id = entityKey.getIdentifier();
3566+
final Object loaded = loadEnhancedEntityUsedAsProxy( entity, session, entityKey );
35783567
if ( loaded == null ) {
35793568
final var persistenceContext = session.getPersistenceContext();
35803569
persistenceContext.removeEntry( entity );
35813570
persistenceContext.removeEntity( entityKey );
3582-
factory.getEntityNotFoundDelegate().handleEntityNotFound( entityKey.getEntityName(), identifier );
3571+
factory.getEntityNotFoundDelegate().handleEntityNotFound( entityKey.getEntityName(), id );
35833572
}
3573+
return readEnhancedEntityAttribute( entity, id, nameOfAttributeBeingAccessed, session );
3574+
}
3575+
else {
3576+
throw new AssertionFailure( "The BytecodeLazyAttributeInterceptor was not an instance of EnhancementAsProxyLazinessInterceptor" );
3577+
}
3578+
}
35843579

3585-
final var interceptor = enhancementMetadata.injectInterceptor( entity, identifier, session );
3586-
3587-
final Object value;
3588-
if ( nameOfAttributeBeingAccessed == null ) {
3589-
return null;
3590-
}
3591-
else if ( interceptor.isAttributeLoaded( nameOfAttributeBeingAccessed ) ) {
3592-
value = getPropertyValue( entity, nameOfAttributeBeingAccessed );
3593-
}
3594-
else {
3595-
value = initializeLazyProperty( nameOfAttributeBeingAccessed, entity, session );
3580+
private Object loadEnhancedEntityUsedAsProxy(
3581+
Object entity,
3582+
SharedSessionContractImplementor session,
3583+
EntityKey entityKey) {
3584+
if ( canReadFromCache && session.isEventSource() ) {
3585+
final Object cachedEntity =
3586+
session.loadFromSecondLevelCache( this, entityKey, entity, LockMode.NONE );
3587+
if ( cachedEntity != null ) {
3588+
return cachedEntity;
35963589
}
3597-
3598-
return interceptor.readObject( entity, nameOfAttributeBeingAccessed, value );
35993590
}
3591+
final var lockOptions = new LockOptions();
3592+
return determineLoaderToUse( session, lockOptions )
3593+
.load( entityKey.getIdentifier(), entity, lockOptions, session );
3594+
}
36003595

3601-
throw new IllegalStateException();
3596+
private Object readEnhancedEntityAttribute(
3597+
Object entity, Object id, String nameOfAttributeBeingAccessed,
3598+
SharedSessionContractImplementor session) {
3599+
final var interceptor =
3600+
getBytecodeEnhancementMetadata()
3601+
.injectInterceptor( entity, id, session );
3602+
final Object value;
3603+
if ( nameOfAttributeBeingAccessed == null ) {
3604+
return null;
3605+
}
3606+
else if ( interceptor.isAttributeLoaded( nameOfAttributeBeingAccessed ) ) {
3607+
value = getPropertyValue( entity, nameOfAttributeBeingAccessed );
3608+
}
3609+
else {
3610+
value = initializeLazyProperty( nameOfAttributeBeingAccessed, entity, session );
3611+
}
3612+
return interceptor.readObject( entity, nameOfAttributeBeingAccessed, value );
36023613
}
36033614

36043615
@Override

0 commit comments

Comments
 (0)