HHH-18583 Add test for issue

This commit is contained in:
Marco Belladelli 2024-09-10 08:57:05 +02:00
parent a26e5059fb
commit 357f32c344
No known key found for this signature in database
GPG Key ID: DBD6E257ABE9EA3D
1 changed files with 38 additions and 3 deletions

View File

@ -38,6 +38,7 @@ import static org.assertj.core.api.Assertions.assertThat;
@SessionFactory( useCollectingStatementInspector = true ) @SessionFactory( useCollectingStatementInspector = true )
@Jira( "https://hibernate.atlassian.net/browse/HHH-17727" ) @Jira( "https://hibernate.atlassian.net/browse/HHH-17727" )
@Jira( "https://hibernate.atlassian.net/browse/HHH-17806" ) @Jira( "https://hibernate.atlassian.net/browse/HHH-17806" )
@Jira( "https://hibernate.atlassian.net/browse/HHH-18583" )
public class JoinedInheritanceDiscriminatorSelectionTest { public class JoinedInheritanceDiscriminatorSelectionTest {
@BeforeAll @BeforeAll
public void setUp(SessionFactoryScope scope) { public void setUp(SessionFactoryScope scope) {
@ -51,7 +52,7 @@ public class JoinedInheritanceDiscriminatorSelectionTest {
@AfterAll @AfterAll
public void tearDown(SessionFactoryScope scope) { public void tearDown(SessionFactoryScope scope) {
scope.inTransaction( session -> session.createMutationQuery( "delete from ParentEntity" ).executeUpdate() ); scope.getSessionFactory().getSchemaManager().truncateMappedObjects();
} }
@Test @Test
@ -93,6 +94,23 @@ public class JoinedInheritanceDiscriminatorSelectionTest {
String.class String.class
).getResultList() ).containsOnly( "parent", "child_a" ); ).getResultList() ).containsOnly( "parent", "child_a" );
inspector.assertNumberOfJoins( 0, 0 ); inspector.assertNumberOfJoins( 0, 0 );
inspector.clear();
// With treat() we preserve the join
assertThat( session.createQuery(
"select p.name from ParentEntity p where treat(p as ChildA).id is not null",
String.class
).getResultList() ).containsExactlyInAnyOrder( "child_a", "sub_child_a" );
inspector.assertNumberOfJoins( 0, 1 );
inspector.clear();
assertThat( session.createQuery(
"select p.name from ParentEntity p where treat(p as ChildB).id is not null",
String.class
).getSingleResult() ).isEqualTo( "child_b" );
inspector.assertNumberOfJoins( 0, 1 );
inspector.clear();
} ); } );
} }
@ -123,8 +141,9 @@ public class JoinedInheritanceDiscriminatorSelectionTest {
inspector.clear(); inspector.clear();
scope.inTransaction( session -> { scope.inTransaction( session -> {
// NOTE: we currently always join all subclasses when selecting the entity instance. We could // With type filters we still join all subclasses when selecting the entity instance
// maybe avoid this when we have a physical discriminator column and a type filter // because we are not aware of the type restriction when processing the selection
assertThat( session.createQuery( assertThat( session.createQuery(
"from ParentEntity p where type(p) = ParentEntity", "from ParentEntity p where type(p) = ParentEntity",
ParentEntity.class ParentEntity.class
@ -144,6 +163,22 @@ public class JoinedInheritanceDiscriminatorSelectionTest {
ParentEntity.class ParentEntity.class
).getResultList() ).hasSize( 1 ); ).getResultList() ).hasSize( 1 );
inspector.assertNumberOfJoins( 0, 3 ); inspector.assertNumberOfJoins( 0, 3 );
inspector.clear();
// With treat() we only join the needed subclasses
assertThat( session.createQuery(
"select treat(p as ChildA) from ParentEntity p",
ParentEntity.class
).getResultList() ).hasSize( 2 );
inspector.assertNumberOfJoins( 0, 2 );
inspector.clear();
assertThat( session.createQuery(
"select treat(p as ChildB) from ParentEntity p",
ParentEntity.class
).getResultList() ).hasSize( 1 );
inspector.assertNumberOfJoins( 0, 1 );
} ); } );
} }