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 void addLazyFieldByGraph(String fieldName) {
} }
mutableLazyFields.add( fieldName ); mutableLazyFields.add( fieldName );
} }
public void clearInitializedLazyFields() {
initializedLazyFields = null;
}
} }

View File

@ -10,6 +10,8 @@
import org.hibernate.NonUniqueObjectException; import org.hibernate.NonUniqueObjectException;
import org.hibernate.TransientObjectException; import org.hibernate.TransientObjectException;
import org.hibernate.UnresolvableObjectException; 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.CollectionDataAccess;
import org.hibernate.cache.spi.access.EntityDataAccess; import org.hibernate.cache.spi.access.EntityDataAccess;
import org.hibernate.cache.spi.access.SoftLock; import org.hibernate.cache.spi.access.SoftLock;
@ -189,6 +191,14 @@ private static void refresh(
EntityEntry entry, EntityEntry entry,
Object id, Object id,
PersistenceContext persistenceContext) { 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( final Object result = source.getLoadQueryInfluencers().fromInternalFetchProfile(
CascadingFetchProfile.REFRESH, CascadingFetchProfile.REFRESH,

View File

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