Skip to content

Commit 274df31

Browse files
marko-bekhtabeikov
authored andcommitted
HHH-19857 Account for nested initialization when loading lazy property
(cherry picked from commit 2aad0da)
1 parent e32cc96 commit 274df31

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

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

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1599,7 +1599,6 @@ private Object initLazyProperties(
15991599

16001600
final var interceptor = asPersistentAttributeInterceptable( entity ).$$_hibernate_getInterceptor();
16011601
assert interceptor != null : "Expecting bytecode interceptor to be non-null";
1602-
final Set<String> initializedLazyAttributeNames = interceptor.getInitializedLazyAttributeNames();
16031602

16041603
final var lazyAttributesMetadata = getBytecodeEnhancementMetadata().getLazyAttributesMetadata();
16051604
final String fetchGroup = lazyAttributesMetadata.getFetchGroupName( fieldName );
@@ -1610,29 +1609,32 @@ private Object initLazyProperties(
16101609
try {
16111610
Object finalResult = null;
16121611
final Object[] results = lazySelect.load( id, session );
1612+
final Set<String> initializedLazyAttributeNames = interceptor.getInitializedLazyAttributeNames();
16131613
int i = 0;
16141614
for ( var fetchGroupAttributeDescriptor : fetchGroupAttributeDescriptors ) {
16151615
final String attributeName = fetchGroupAttributeDescriptor.getName();
1616-
final boolean previousInitialized = initializedLazyAttributeNames.contains( attributeName );
1617-
if ( previousInitialized ) {
1618-
// it's already been initialized (e.g. by a write) so we don't want to overwrite
1619-
i++;
1620-
// TODO: we should consider un-marking an attribute as dirty based on the selected value
1621-
// - we know the current value:
1622-
// getPropertyValue( entity, fetchGroupAttributeDescriptor.getAttributeIndex() );
1623-
// - we know the selected value (see selectedValue below)
1624-
// - we can use the attribute Type to tell us if they are the same
1625-
// - assuming entity is a SelfDirtinessTracker we can also know if the attribute is currently
1626-
// considered dirty, and if really not dirty we would do the un-marking
1627-
// - of course that would mean a new method on SelfDirtinessTracker to allow un-marking
1616+
if ( fieldName.equals( attributeName ) ) {
1617+
finalResult = results[i];
16281618
}
1629-
else {
1630-
final Object result = results[i++];
1631-
if ( initializeLazyProperty( fieldName, entity, entry, fetchGroupAttributeDescriptor, result ) ) {
1632-
finalResult = result;
1633-
interceptor.attributeInitialized( attributeName );
1634-
}
1619+
if ( !initializedLazyAttributeNames.contains( attributeName ) ) {
1620+
initializeLazyProperty(
1621+
entity,
1622+
entry,
1623+
results[i],
1624+
getPropertyIndex( attributeName ),
1625+
fetchGroupAttributeDescriptor.getType()
1626+
);
16351627
}
1628+
// if the attribute has already been initialized (e.g. by a write) we don't want to overwrite
1629+
i++;
1630+
// TODO: we should consider un-marking an attribute as dirty based on the selected value
1631+
// - we know the current value:
1632+
// getPropertyValue( entity, fetchGroupAttributeDescriptor.getAttributeIndex() );
1633+
// - we know the selected value (see selectedValue below)
1634+
// - we can use the attribute Type to tell us if they are the same
1635+
// - assuming entity is a SelfDirtinessTracker we can also know if the attribute is currently
1636+
// considered dirty, and if really not dirty we would do the un-marking
1637+
// - of course that would mean a new method on SelfDirtinessTracker to allow un-marking
16361638
}
16371639
LOG.trace( "Done initializing lazy properties" );
16381640
return finalResult;
@@ -1754,7 +1756,11 @@ protected boolean initializeLazyProperty(
17541756
return fieldName.equals( lazyPropertyNames[index] );
17551757
}
17561758

1757-
// Used by Hibernate Reactive
1759+
/**
1760+
* Used by Hibernate Reactive
1761+
* @deprecated
1762+
*/
1763+
@Deprecated(since = "7.2", forRemoval = true)
17581764
protected boolean initializeLazyProperty(
17591765
final String fieldName,
17601766
final Object entity,

0 commit comments

Comments
 (0)