3838@ SessionFactory ( useCollectingStatementInspector = true )
3939@ Jira ( "https://hibernate.atlassian.net/browse/HHH-17727" )
4040@ Jira ( "https://hibernate.atlassian.net/browse/HHH-17806" )
41+ @ Jira ( "https://hibernate.atlassian.net/browse/HHH-18583" )
4142public class JoinedInheritanceDiscriminatorSelectionTest {
4243 @ BeforeAll
4344 public void setUp (SessionFactoryScope scope ) {
@@ -51,7 +52,7 @@ public void setUp(SessionFactoryScope scope) {
5152
5253 @ AfterAll
5354 public void tearDown (SessionFactoryScope scope ) {
54- scope .inTransaction ( session -> session . createMutationQuery ( "delete from ParentEntity" ). executeUpdate () );
55+ scope .getSessionFactory (). getSchemaManager (). truncateMappedObjects ( );
5556 }
5657
5758 @ Test
@@ -93,6 +94,23 @@ public void testSelectParentAttribute(SessionFactoryScope scope) {
9394 String .class
9495 ).getResultList () ).containsOnly ( "parent" , "child_a" );
9596 inspector .assertNumberOfJoins ( 0 , 0 );
97+ inspector .clear ();
98+
99+ // With treat() we preserve the join
100+
101+ assertThat ( session .createQuery (
102+ "select p.name from ParentEntity p where treat(p as ChildA).id is not null" ,
103+ String .class
104+ ).getResultList () ).containsExactlyInAnyOrder ( "child_a" , "sub_child_a" );
105+ inspector .assertNumberOfJoins ( 0 , 1 );
106+ inspector .clear ();
107+
108+ assertThat ( session .createQuery (
109+ "select p.name from ParentEntity p where treat(p as ChildB).id is not null" ,
110+ String .class
111+ ).getSingleResult () ).isEqualTo ( "child_b" );
112+ inspector .assertNumberOfJoins ( 0 , 1 );
113+ inspector .clear ();
96114 } );
97115 }
98116
@@ -123,8 +141,9 @@ public void testSelectInstance(SessionFactoryScope scope) {
123141 inspector .clear ();
124142
125143 scope .inTransaction ( session -> {
126- // NOTE: we currently always join all subclasses when selecting the entity instance. We could
127- // maybe avoid this when we have a physical discriminator column and a type filter
144+ // With type filters we still join all subclasses when selecting the entity instance
145+ // because we are not aware of the type restriction when processing the selection
146+
128147 assertThat ( session .createQuery (
129148 "from ParentEntity p where type(p) = ParentEntity" ,
130149 ParentEntity .class
@@ -144,6 +163,22 @@ public void testSelectInstance(SessionFactoryScope scope) {
144163 ParentEntity .class
145164 ).getResultList () ).hasSize ( 1 );
146165 inspector .assertNumberOfJoins ( 0 , 3 );
166+ inspector .clear ();
167+
168+ // With treat() we only join the needed subclasses
169+
170+ assertThat ( session .createQuery (
171+ "select treat(p as ChildA) from ParentEntity p" ,
172+ ParentEntity .class
173+ ).getResultList () ).hasSize ( 2 );
174+ inspector .assertNumberOfJoins ( 0 , 2 );
175+ inspector .clear ();
176+
177+ assertThat ( session .createQuery (
178+ "select treat(p as ChildB) from ParentEntity p" ,
179+ ParentEntity .class
180+ ).getResultList () ).hasSize ( 1 );
181+ inspector .assertNumberOfJoins ( 0 , 1 );
147182 } );
148183 }
149184
0 commit comments