HHH-16184 Add test for issue

This commit is contained in:
Andrea Boriero 2023-02-14 16:35:11 +01:00 committed by Andrea Boriero
parent 7e629957e4
commit 2ee1c970a3
2 changed files with 134 additions and 13 deletions

View File

@ -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<Parent> results = session.createQuery(
"select p from Parent p",
Parent.class
)
.scroll()) {
List<Parent> 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 );
}
}

View File

@ -3,17 +3,17 @@
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping default-lazy="false" package="org.hibernate.orm.test.collection.list">
<class name="Parent" table="custom_field">
<class name="Parent" table="parent_table">
<id name="id"/>
<property name="name" column="name"/>
<list name="children" inverse="true" cascade="all-delete-orphan">
<key column="custom_field_id"/>
<key column="parent_id"/>
<index column="seq_num"/>
<one-to-many class="Child"/>
</list>
</class>
<class name="Child" table="custom_field_option">
<class name="Child" table="child_table">
<id name="id"/>
<many-to-one name="parent" not-null="true" column="custom_field_id"/>
<many-to-one name="parent" not-null="true" column="parent_id"/>
</class>
</hibernate-mapping>