|
| 1 | +package org.hibernate.orm.test.engine.spi; |
| 2 | + |
| 3 | +import java.util.Set; |
| 4 | + |
| 5 | +import org.hibernate.annotations.BatchSize; |
| 6 | +import org.hibernate.cfg.AvailableSettings; |
| 7 | +import org.hibernate.engine.spi.LoadQueryInfluencers; |
| 8 | +import org.hibernate.metamodel.model.domain.NavigableRole; |
| 9 | +import org.hibernate.persister.collection.CollectionPersister; |
| 10 | +import org.hibernate.persister.entity.EntityPersister; |
| 11 | + |
| 12 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 13 | +import org.hibernate.testing.orm.junit.ServiceRegistry; |
| 14 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 15 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 16 | +import org.hibernate.testing.orm.junit.Setting; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | + |
| 19 | +import jakarta.persistence.Entity; |
| 20 | +import jakarta.persistence.GeneratedValue; |
| 21 | +import jakarta.persistence.GenerationType; |
| 22 | +import jakarta.persistence.Id; |
| 23 | +import jakarta.persistence.OneToMany; |
| 24 | +import jakarta.persistence.Table; |
| 25 | + |
| 26 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 27 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 28 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 29 | + |
| 30 | +@DomainModel(annotatedClasses = { |
| 31 | + LoadQueryInfluencersTest.EntityWithBatchSize1.class, |
| 32 | + LoadQueryInfluencersTest.ChildEntity.class |
| 33 | +}) |
| 34 | +@SessionFactory |
| 35 | +@ServiceRegistry( |
| 36 | + settings = { |
| 37 | + @Setting(name = AvailableSettings.DEFAULT_BATCH_FETCH_SIZE, value = "100") |
| 38 | + } |
| 39 | +) |
| 40 | +public class LoadQueryInfluencersTest { |
| 41 | + |
| 42 | + @Test |
| 43 | + public void usesCustomEntityBatchSizeForEffectivelyBatchLoadable(SessionFactoryScope scope) { |
| 44 | + scope.inSession( |
| 45 | + (nonTransactedSession) -> { |
| 46 | + LoadQueryInfluencers influencers = new LoadQueryInfluencers( nonTransactedSession.getSessionFactory() ); |
| 47 | + assertTrue( influencers.getBatchSize() > 1, "Expecting default batch size > 1" ); |
| 48 | + |
| 49 | + EntityPersister persister = nonTransactedSession.getSessionFactory() |
| 50 | + .getRuntimeMetamodels() |
| 51 | + .getMappingMetamodel() |
| 52 | + .getEntityDescriptor( EntityWithBatchSize1.class ); |
| 53 | + |
| 54 | + assertFalse( persister.isBatchLoadable(), "Incorrect persister batch loadable." ); |
| 55 | + assertEquals( 1, influencers.effectiveBatchSize( persister ), "Incorrect effective batch size." ); |
| 56 | + assertFalse( |
| 57 | + influencers.effectivelyBatchLoadable( persister ), |
| 58 | + "Incorrect effective batch loadable." |
| 59 | + ); |
| 60 | + } |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void usesCustomCollectionBatchSizeForEffectivelyBatchLoadable(SessionFactoryScope scope) { |
| 66 | + scope.inSession( |
| 67 | + (nonTransactedSession) -> { |
| 68 | + LoadQueryInfluencers influencers = new LoadQueryInfluencers( nonTransactedSession.getSessionFactory() ); |
| 69 | + assertTrue( influencers.getBatchSize() > 1, "Expecting default batch size > 1" ); |
| 70 | + |
| 71 | + EntityPersister entityPersister = nonTransactedSession.getSessionFactory() |
| 72 | + .getRuntimeMetamodels() |
| 73 | + .getMappingMetamodel() |
| 74 | + .getEntityDescriptor( EntityWithBatchSize1.class ); |
| 75 | + |
| 76 | + NavigableRole collectionRole = entityPersister.getNavigableRole() |
| 77 | + .append( "childrenWithBatchSize1" ); |
| 78 | + |
| 79 | + CollectionPersister persister = nonTransactedSession.getSessionFactory() |
| 80 | + .getRuntimeMetamodels() |
| 81 | + .getMappingMetamodel() |
| 82 | + .findCollectionDescriptor( collectionRole.getFullPath() ); |
| 83 | + |
| 84 | + assertFalse( persister.isBatchLoadable(), "Incorrect persister batch loadable." ); |
| 85 | + assertEquals( 1, influencers.effectiveBatchSize( persister ), "Incorrect effective batch size." ); |
| 86 | + assertFalse( |
| 87 | + influencers.effectivelyBatchLoadable( persister ), |
| 88 | + "Incorrect effective batch loadable." |
| 89 | + ); |
| 90 | + } |
| 91 | + ); |
| 92 | + } |
| 93 | + |
| 94 | + |
| 95 | + @Entity |
| 96 | + @Table(name = "EntityWithBatchSize1") |
| 97 | + @BatchSize(size = 1) |
| 98 | + public class EntityWithBatchSize1 { |
| 99 | + @Id |
| 100 | + @GeneratedValue(strategy = GenerationType.IDENTITY) |
| 101 | + private Long id; |
| 102 | + |
| 103 | + @OneToMany |
| 104 | + @BatchSize(size = 1) |
| 105 | + private Set<ChildEntity> childrenWithBatchSize1; |
| 106 | + |
| 107 | + public Long getId() { |
| 108 | + return id; |
| 109 | + } |
| 110 | + |
| 111 | + public void setId(Long id) { |
| 112 | + this.id = id; |
| 113 | + } |
| 114 | + |
| 115 | + public Set<ChildEntity> getChildrenWithBatchSize1() { |
| 116 | + return childrenWithBatchSize1; |
| 117 | + } |
| 118 | + |
| 119 | + public void setChildrenWithBatchSize1(Set<ChildEntity> childrenWithBatchSize1) { |
| 120 | + this.childrenWithBatchSize1 = childrenWithBatchSize1; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + @Entity |
| 125 | + @Table(name = "ChildEntity") |
| 126 | + public class ChildEntity { |
| 127 | + @Id |
| 128 | + @GeneratedValue(strategy = GenerationType.IDENTITY) |
| 129 | + private Long id; |
| 130 | + |
| 131 | + public Long getId() { |
| 132 | + return id; |
| 133 | + } |
| 134 | + |
| 135 | + public void setId(Long id) { |
| 136 | + this.id = id; |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments