From 98d9ac365498baf77372ba26e4e5c8551a7d30b0 Mon Sep 17 00:00:00 2001 From: Noel Trout Date: Thu, 5 Sep 2024 10:55:41 +0200 Subject: [PATCH] HHH-18571 Added test case demonstrating issue --- .../engine/spi/LoadQueryInfluencersTest.java | 139 ++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/engine/spi/LoadQueryInfluencersTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/engine/spi/LoadQueryInfluencersTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/engine/spi/LoadQueryInfluencersTest.java new file mode 100644 index 0000000000..218093c740 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/engine/spi/LoadQueryInfluencersTest.java @@ -0,0 +1,139 @@ +package org.hibernate.orm.test.engine.spi; + +import java.util.Set; + +import org.hibernate.annotations.BatchSize; +import org.hibernate.cfg.AvailableSettings; +import org.hibernate.engine.spi.LoadQueryInfluencers; +import org.hibernate.metamodel.model.domain.NavigableRole; +import org.hibernate.persister.collection.CollectionPersister; +import org.hibernate.persister.entity.EntityPersister; + +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.Test; + +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.OneToMany; +import jakarta.persistence.Table; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +@DomainModel(annotatedClasses = { + LoadQueryInfluencersTest.EntityWithBatchSize1.class, + LoadQueryInfluencersTest.ChildEntity.class +}) +@SessionFactory +@ServiceRegistry( + settings = { + @Setting(name = AvailableSettings.DEFAULT_BATCH_FETCH_SIZE, value = "100") + } +) +public class LoadQueryInfluencersTest { + + @Test + public void usesCustomEntityBatchSizeForEffectivelyBatchLoadable(SessionFactoryScope scope) { + scope.inSession( + (nonTransactedSession) -> { + LoadQueryInfluencers influencers = new LoadQueryInfluencers( nonTransactedSession.getSessionFactory() ); + assertTrue( influencers.getBatchSize() > 1, "Expecting default batch size > 1" ); + + EntityPersister persister = nonTransactedSession.getSessionFactory() + .getRuntimeMetamodels() + .getMappingMetamodel() + .getEntityDescriptor( EntityWithBatchSize1.class ); + + assertFalse( persister.isBatchLoadable(), "Incorrect persister batch loadable." ); + assertEquals( 1, influencers.effectiveBatchSize( persister ), "Incorrect effective batch size." ); + assertFalse( + influencers.effectivelyBatchLoadable( persister ), + "Incorrect effective batch loadable." + ); + } + ); + } + + @Test + public void usesCustomCollectionBatchSizeForEffectivelyBatchLoadable(SessionFactoryScope scope) { + scope.inSession( + (nonTransactedSession) -> { + LoadQueryInfluencers influencers = new LoadQueryInfluencers( nonTransactedSession.getSessionFactory() ); + assertTrue( influencers.getBatchSize() > 1, "Expecting default batch size > 1" ); + + EntityPersister entityPersister = nonTransactedSession.getSessionFactory() + .getRuntimeMetamodels() + .getMappingMetamodel() + .getEntityDescriptor( EntityWithBatchSize1.class ); + + NavigableRole collectionRole = entityPersister.getNavigableRole() + .append( "childrenWithBatchSize1" ); + + CollectionPersister persister = nonTransactedSession.getSessionFactory() + .getRuntimeMetamodels() + .getMappingMetamodel() + .findCollectionDescriptor( collectionRole.getFullPath() ); + + assertFalse( persister.isBatchLoadable(), "Incorrect persister batch loadable." ); + assertEquals( 1, influencers.effectiveBatchSize( persister ), "Incorrect effective batch size." ); + assertFalse( + influencers.effectivelyBatchLoadable( persister ), + "Incorrect effective batch loadable." + ); + } + ); + } + + + @Entity + @Table(name = "EntityWithBatchSize1") + @BatchSize(size = 1) + public class EntityWithBatchSize1 { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @OneToMany + @BatchSize(size = 1) + private Set childrenWithBatchSize1; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public Set getChildrenWithBatchSize1() { + return childrenWithBatchSize1; + } + + public void setChildrenWithBatchSize1(Set childrenWithBatchSize1) { + this.childrenWithBatchSize1 = childrenWithBatchSize1; + } + } + + @Entity + @Table(name = "ChildEntity") + public class ChildEntity { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + } +}