HHH-13377 Lazy loaded properties of bytecode enhanced entity are left stale after refresh of entity

This commit is contained in:
Andrea Boriero 2024-12-03 18:34:23 +01:00 committed by Andrea Boriero
parent ad5855f4b1
commit 4ebbf5b36d
3 changed files with 15 additions and 3 deletions

View File

@ -198,4 +198,8 @@ public class LazyAttributeLoadingInterceptor extends AbstractLazyLoadInterceptor
}
mutableLazyFields.add( fieldName );
}
public void clearInitializedLazyFields() {
initializedLazyFields = null;
}
}

View File

@ -10,6 +10,8 @@ import org.hibernate.LockOptions;
import org.hibernate.NonUniqueObjectException;
import org.hibernate.TransientObjectException;
import org.hibernate.UnresolvableObjectException;
import org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeLoadingInterceptor;
import org.hibernate.bytecode.spi.BytecodeEnhancementMetadata;
import org.hibernate.cache.spi.access.CollectionDataAccess;
import org.hibernate.cache.spi.access.EntityDataAccess;
import org.hibernate.cache.spi.access.SoftLock;
@ -189,6 +191,14 @@ public class DefaultRefreshEventListener implements RefreshEventListener {
EntityEntry entry,
Object id,
PersistenceContext persistenceContext) {
final BytecodeEnhancementMetadata instrumentationMetadata = persister.getInstrumentationMetadata();
if ( object != null && instrumentationMetadata.isEnhancedForLazyLoading() ) {
final LazyAttributeLoadingInterceptor interceptor = instrumentationMetadata.extractInterceptor( object );
if ( interceptor != null ) {
// The list of initialized lazy fields have to be cleared in order to refresh them from the database.
interceptor.clearInitializedLazyFields();
}
}
final Object result = source.getLoadQueryInfluencers().fromInternalFetchProfile(
CascadingFetchProfile.REFRESH,

View File

@ -13,7 +13,6 @@ import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import org.hibernate.annotations.Formula;
import org.hibernate.dialect.MariaDBDialect;
import org.hibernate.dialect.MySQLDialect;
import org.hibernate.dialect.SQLServerDialect;
import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced;
@ -44,8 +43,7 @@ import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
@BytecodeEnhanced
@SkipForDialectGroup(
{
@SkipForDialect( dialectClass = MySQLDialect.class, reason = "does not support || as String concatenation"),
@SkipForDialect( dialectClass = MariaDBDialect.class, reason = "does not support || as String concatenation"),
@SkipForDialect( dialectClass = MySQLDialect.class, matchSubTypes = true, reason = "does not support || as String concatenation"),
@SkipForDialect( dialectClass = SQLServerDialect.class, reason = "does not support || as String concatenation"),
}
)