From 66ef96532027ed4bc6045b79dfbb0ab39939d138 Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Tue, 14 Feb 2023 16:35:11 +0100 Subject: [PATCH] HHH-16184 Add test for issue --- .../IterateOverListInTheSetMethodTest.java | 139 ++++++++++++++++-- .../list/ParentChildMapping.hbm.xml | 8 +- 2 files changed, 134 insertions(+), 13 deletions(-) diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/collection/list/IterateOverListInTheSetMethodTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/collection/list/IterateOverListInTheSetMethodTest.java index 3e2c3998a3..94e0298736 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/collection/list/IterateOverListInTheSetMethodTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/collection/list/IterateOverListInTheSetMethodTest.java @@ -1,19 +1,40 @@ package org.hibernate.orm.test.collection.list; +import java.util.ArrayList; +import java.util.List; + +import org.hibernate.query.spi.ScrollableResultsImplementor; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.jdbc.SQLStatementInspector; import org.hibernate.testing.orm.junit.DomainModel; import org.hibernate.testing.orm.junit.SessionFactory; import org.hibernate.testing.orm.junit.SessionFactoryScope; -import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + @DomainModel( xmlMappings = "org/hibernate/orm/test/collection/list/ParentChildMapping.hbm.xml" ) @SessionFactory public class IterateOverListInTheSetMethodTest { - @BeforeAll - public void setUp(SessionFactoryScope scope) { + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + session.createMutationQuery( "delete from Child" ).executeUpdate(); + session.createMutationQuery( "delete from Parent" ).executeUpdate(); + } + ); + } + + + @Test + @TestForIssue(jiraKey = "HHH-16120") + public void testHqlQuery(SessionFactoryScope scope) { scope.inTransaction( session -> { Child child = new Child( 1, "Luigi" ); @@ -27,14 +48,114 @@ public class IterateOverListInTheSetMethodTest { session.persist( child2 ); } ); - } - - @Test - public void testHqlQuery(SessionFactoryScope scope) { - scope.inSession( + scope.inTransaction( session -> { - session.createQuery( "select p from Parent p" ).list(); + session.createQuery( "select p from Parent p", Parent.class ).list(); } ); } + + @Test + @TestForIssue(jiraKey = "HHH-16184") + public void testSelectParentsWithoutChildren(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + Parent parent = new Parent( 2, "Fabio" ); + session.persist( parent ); + } + ); + + SQLStatementInspector collectingStatementInspector = scope.getCollectingStatementInspector(); + collectingStatementInspector.clear(); + scope.inTransaction( + session -> { + session.createQuery( "select p from Parent p", Parent.class ).list(); + + } + ); + assertThat( collectingStatementInspector.getSqlQueries().size() ).isEqualTo( 2 ); + } + + @Test + @TestForIssue(jiraKey = "HHH-16184") + public void testScrollParentsWithoutChildren(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + Parent parent = new Parent( 2, "Fabio" ); + session.persist( parent ); + } + ); + + SQLStatementInspector collectingStatementInspector = scope.getCollectingStatementInspector(); + collectingStatementInspector.clear(); + scope.inTransaction( + session -> { + try (ScrollableResultsImplementor results = session.createQuery( + "select p from Parent p", + Parent.class + ) + .scroll()) { + List list = new ArrayList<>(); + while ( results.next() ) { + list.add( results.get() ); + } + assertThat( list.size() ).isEqualTo( 1 ); + } + + } + ); + assertThat( collectingStatementInspector.getSqlQueries().size() ).isEqualTo( 2 ); + } + + @Test + @TestForIssue(jiraKey = "HHH-16184") + public void testSelectParentsWithoutChildren2(SessionFactoryScope scope) { + Integer parentId = 2; + scope.inTransaction( + session -> { + Parent parent = new Parent( parentId, "Fabio" ); + session.persist( parent ); + } + ); + + SQLStatementInspector collectingStatementInspector = scope.getCollectingStatementInspector(); + collectingStatementInspector.clear(); + scope.inTransaction( + session -> { + session.createQuery( "select p from Parent p where p.id = :id", Parent.class ) + .setParameter( "id", parentId ) + .uniqueResult(); + + } + ); + assertThat( collectingStatementInspector.getSqlQueries().size() ).isEqualTo( 2 ); + } + + @Test + @TestForIssue(jiraKey = "HHH-16184") + public void testSelectParentsWithChildren(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + Child child = new Child( 1, "Luigi" ); + Child child2 = new Child( 2, "Franco" ); + Parent parent = new Parent( 2, "Fabio" ); + parent.addChild( child ); + parent.addChild( child2 ); + + session.persist( parent ); + session.persist( child ); + session.persist( child2 ); + } + ); + + SQLStatementInspector collectingStatementInspector = scope.getCollectingStatementInspector(); + collectingStatementInspector.clear(); + scope.inTransaction( + session -> { + session.createQuery( "select p from Parent p", Parent.class ).list(); + + } + ); + assertThat( collectingStatementInspector.getSqlQueries().size() ).isEqualTo( 2 ); + } } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/collection/list/ParentChildMapping.hbm.xml b/hibernate-core/src/test/java/org/hibernate/orm/test/collection/list/ParentChildMapping.hbm.xml index 77bcc3ad02..9e94d3bad7 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/collection/list/ParentChildMapping.hbm.xml +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/collection/list/ParentChildMapping.hbm.xml @@ -3,17 +3,17 @@ "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> - + - + - + - + \ No newline at end of file