From e6fbfad0b6ac6837eb578d1beec8faab525da47c Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Mon, 5 Jun 2023 16:26:54 +0200 Subject: [PATCH] HHH-16750 Add test for issue --- ...iteIdAndElementCollectionBatchingTest.java | 37 ++++- ...FieldAndElementCollectionBatchingTest.java | 129 ++++++++++++++++++ 2 files changed, 161 insertions(+), 5 deletions(-) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/batch/CompositeIdWithOneFieldAndElementCollectionBatchingTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/batch/CompositeIdAndElementCollectionBatchingTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/batch/CompositeIdAndElementCollectionBatchingTest.java index fd54037c2f..8d077a249b 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/batch/CompositeIdAndElementCollectionBatchingTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/batch/CompositeIdAndElementCollectionBatchingTest.java @@ -15,6 +15,7 @@ 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.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -46,13 +47,28 @@ class CompositeIdAndElementCollectionBatchingTest { new EntityId( "EntityA", ENTITY_B ), Collections.singleton( new EmbeddableA( "EmbeddableA" ) ) ); + private static final EntityA ENTITY_A2 = new EntityA( + new EntityId( "EntityA2", ENTITY_B ), + Collections.singleton( new EmbeddableA( "EmbeddableB" ) ) + ); @BeforeEach public void setUp(SessionFactoryScope scope) { scope.inTransaction( - entityManager -> { - entityManager.persist( ENTITY_B ); - entityManager.persist( ENTITY_A ); + session -> { + session.persist( ENTITY_B ); + session.persist( ENTITY_A ); + session.persist( ENTITY_A2 ); + } + ); + } + + @AfterEach + public void tearDown(SessionFactoryScope scope){ + scope.inTransaction( + session -> { + session.createQuery( "delete from EntityA" ).executeUpdate(); + session.createQuery( "delete from EntityB" ).executeUpdate(); } ); } @@ -60,8 +76,19 @@ class CompositeIdAndElementCollectionBatchingTest { @Test void testSelect(SessionFactoryScope scope) { scope.inTransaction( - entityManager -> { - EntityA a = entityManager.find( EntityA.class, ENTITY_A.id ); + session -> { + EntityA a = session.find( EntityA.class, ENTITY_A.id ); + assertThat( a.elementCollection ).hasSize( 1 ); + } + ); + } + + @Test + void testSelect2(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + EntityA a = session.find( EntityA.class, ENTITY_A.id ); + EntityA a2 = session.find( EntityA.class, ENTITY_A2.id ); assertThat( a.elementCollection ).hasSize( 1 ); } ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/batch/CompositeIdWithOneFieldAndElementCollectionBatchingTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/batch/CompositeIdWithOneFieldAndElementCollectionBatchingTest.java new file mode 100644 index 0000000000..af036e4734 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/batch/CompositeIdWithOneFieldAndElementCollectionBatchingTest.java @@ -0,0 +1,129 @@ +package org.hibernate.orm.test.batch; + +import java.io.Serializable; +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; + +import org.hibernate.cfg.Environment; + +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +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.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import jakarta.persistence.ElementCollection; +import jakarta.persistence.Embeddable; +import jakarta.persistence.EmbeddedId; +import jakarta.persistence.Entity; + +import static org.assertj.core.api.Assertions.assertThat; + +@DomainModel( + annotatedClasses = { + CompositeIdWithOneFieldAndElementCollectionBatchingTest.EntityA.class, + } +) +@SessionFactory +@ServiceRegistry( + settings = @Setting(name = Environment.DEFAULT_BATCH_FETCH_SIZE, value = "2") +) +@JiraKey("HHH-16750") +class CompositeIdWithOneFieldAndElementCollectionBatchingTest { + + private static final EntityA ENTITY_A = new EntityA( + new EntityId( "EntityA" ), + Collections.singleton( new EmbeddableA( "EmbeddableA" ) ) + ); + + private static final EntityA ENTITY_A2 = new EntityA( + new EntityId( "EntityB" ), + Collections.singleton( new EmbeddableA( "EmbeddableB" ) ) + ); + + @BeforeEach + public void setUp(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + session.persist( ENTITY_A ); + session.persist( ENTITY_A2 ); + } + ); + } + + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( + session -> + session.createQuery( "delete from EntityA" ).executeUpdate() + ); + } + + @Test + void testSelect(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + EntityA a = session.find( EntityA.class, ENTITY_A.id ); + assertThat( a.elementCollection ).hasSize( 1 ); + } + ); + } + + @Test + void testSelect2(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + EntityA a = session.find( EntityA.class, ENTITY_A.id ); + EntityA a2 = session.find( EntityA.class, ENTITY_A2.id ); + assertThat( a.elementCollection ).hasSize( 1 ); + } + ); + } + + @Entity(name = "EntityA") + public static class EntityA { + @EmbeddedId + private EntityId id; + + @ElementCollection + public Set elementCollection = new HashSet<>(); + + public EntityA() { + } + + public EntityA(EntityId id, Set elementCollection) { + this.id = id; + this.elementCollection = elementCollection; + } + } + + @Embeddable + public static class EntityId implements Serializable { + private String id1; + + public EntityId() { + } + + public EntityId(String id1) { + this.id1 = id1; + } + } + + @Embeddable + public static class EmbeddableA { + private String name; + + public EmbeddableA() { + } + + public EmbeddableA(String name) { + this.name = name; + } + } + +}