Skip to content

Commit 4ebbf5b

Browse files
committed
HHH-13377 Lazy loaded properties of bytecode enhanced entity are left stale after refresh of entity
1 parent ad5855f commit 4ebbf5b

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

hibernate-core/src/main/java/org/hibernate/bytecode/enhance/spi/interceptor/LazyAttributeLoadingInterceptor.java

+4
Original file line numberDiff line numberDiff line change
@@ -198,4 +198,8 @@ public void addLazyFieldByGraph(String fieldName) {
198198
}
199199
mutableLazyFields.add( fieldName );
200200
}
201+
202+
public void clearInitializedLazyFields() {
203+
initializedLazyFields = null;
204+
}
201205
}

hibernate-core/src/main/java/org/hibernate/event/internal/DefaultRefreshEventListener.java

+10
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import org.hibernate.NonUniqueObjectException;
1111
import org.hibernate.TransientObjectException;
1212
import org.hibernate.UnresolvableObjectException;
13+
import org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeLoadingInterceptor;
14+
import org.hibernate.bytecode.spi.BytecodeEnhancementMetadata;
1315
import org.hibernate.cache.spi.access.CollectionDataAccess;
1416
import org.hibernate.cache.spi.access.EntityDataAccess;
1517
import org.hibernate.cache.spi.access.SoftLock;
@@ -189,6 +191,14 @@ private static void refresh(
189191
EntityEntry entry,
190192
Object id,
191193
PersistenceContext persistenceContext) {
194+
final BytecodeEnhancementMetadata instrumentationMetadata = persister.getInstrumentationMetadata();
195+
if ( object != null && instrumentationMetadata.isEnhancedForLazyLoading() ) {
196+
final LazyAttributeLoadingInterceptor interceptor = instrumentationMetadata.extractInterceptor( object );
197+
if ( interceptor != null ) {
198+
// The list of initialized lazy fields have to be cleared in order to refresh them from the database.
199+
interceptor.clearInitializedLazyFields();
200+
}
201+
}
192202

193203
final Object result = source.getLoadQueryInfluencers().fromInternalFetchProfile(
194204
CascadingFetchProfile.REFRESH,

hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/refresh/RefreshEntityWithLazyPropertyTest.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import jakarta.persistence.OneToMany;
1414
import jakarta.persistence.Table;
1515
import org.hibernate.annotations.Formula;
16-
import org.hibernate.dialect.MariaDBDialect;
1716
import org.hibernate.dialect.MySQLDialect;
1817
import org.hibernate.dialect.SQLServerDialect;
1918
import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced;
@@ -44,8 +43,7 @@
4443
@BytecodeEnhanced
4544
@SkipForDialectGroup(
4645
{
47-
@SkipForDialect( dialectClass = MySQLDialect.class, reason = "does not support || as String concatenation"),
48-
@SkipForDialect( dialectClass = MariaDBDialect.class, reason = "does not support || as String concatenation"),
46+
@SkipForDialect( dialectClass = MySQLDialect.class, matchSubTypes = true, reason = "does not support || as String concatenation"),
4947
@SkipForDialect( dialectClass = SQLServerDialect.class, reason = "does not support || as String concatenation"),
5048
}
5149
)

0 commit comments

Comments
 (0)