diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/inheritance/discriminator/JoinedInheritanceDiscriminatorSelectionTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/inheritance/discriminator/JoinedInheritanceDiscriminatorSelectionTest.java index 5a2035d021..1657f6be1a 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/inheritance/discriminator/JoinedInheritanceDiscriminatorSelectionTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/inheritance/discriminator/JoinedInheritanceDiscriminatorSelectionTest.java @@ -36,6 +36,7 @@ import static org.assertj.core.api.Assertions.assertThat; @SessionFactory( useCollectingStatementInspector = true ) @Jira( "https://hibernate.atlassian.net/browse/HHH-17727" ) @Jira( "https://hibernate.atlassian.net/browse/HHH-17806" ) +@Jira( "https://hibernate.atlassian.net/browse/HHH-18583" ) public class JoinedInheritanceDiscriminatorSelectionTest { @BeforeAll public void setUp(SessionFactoryScope scope) { @@ -49,7 +50,7 @@ public class JoinedInheritanceDiscriminatorSelectionTest { @AfterAll public void tearDown(SessionFactoryScope scope) { - scope.inTransaction( session -> session.createMutationQuery( "delete from ParentEntity" ).executeUpdate() ); + scope.getSessionFactory().getSchemaManager().truncateMappedObjects(); } @Test @@ -91,6 +92,23 @@ public class JoinedInheritanceDiscriminatorSelectionTest { String.class ).getResultList() ).containsOnly( "parent", "child_a" ); 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(); } ); } @@ -121,8 +139,9 @@ public class JoinedInheritanceDiscriminatorSelectionTest { inspector.clear(); scope.inTransaction( session -> { - // NOTE: we currently always join all subclasses when selecting the entity instance. We could - // maybe avoid this when we have a physical discriminator column and a type filter + // With type filters we still join all subclasses when selecting the entity instance + // because we are not aware of the type restriction when processing the selection + assertThat( session.createQuery( "from ParentEntity p where type(p) = ParentEntity", ParentEntity.class @@ -142,6 +161,22 @@ public class JoinedInheritanceDiscriminatorSelectionTest { ParentEntity.class ).getResultList() ).hasSize( 1 ); 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 ); } ); }