Skip to content

Commit fc33ed8

Browse files
mbelladesebersole
authored andcommitted
HHH-18583 Add test for issue
1 parent 8dca863 commit fc33ed8

File tree

1 file changed

+38
-3
lines changed

1 file changed

+38
-3
lines changed

hibernate-core/src/test/java/org/hibernate/orm/test/inheritance/discriminator/JoinedInheritanceDiscriminatorSelectionTest.java

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
@SessionFactory( useCollectingStatementInspector = true )
3737
@Jira( "https://hibernate.atlassian.net/browse/HHH-17727" )
3838
@Jira( "https://hibernate.atlassian.net/browse/HHH-17806" )
39+
@Jira( "https://hibernate.atlassian.net/browse/HHH-18583" )
3940
public class JoinedInheritanceDiscriminatorSelectionTest {
4041
@BeforeAll
4142
public void setUp(SessionFactoryScope scope) {
@@ -49,7 +50,7 @@ public void setUp(SessionFactoryScope scope) {
4950

5051
@AfterAll
5152
public void tearDown(SessionFactoryScope scope) {
52-
scope.inTransaction( session -> session.createMutationQuery( "delete from ParentEntity" ).executeUpdate() );
53+
scope.getSessionFactory().getSchemaManager().truncateMappedObjects();
5354
}
5455

5556
@Test
@@ -91,6 +92,23 @@ public void testSelectParentAttribute(SessionFactoryScope scope) {
9192
String.class
9293
).getResultList() ).containsOnly( "parent", "child_a" );
9394
inspector.assertNumberOfJoins( 0, 0 );
95+
inspector.clear();
96+
97+
// With treat() we preserve the join
98+
99+
assertThat( session.createQuery(
100+
"select p.name from ParentEntity p where treat(p as ChildA).id is not null",
101+
String.class
102+
).getResultList() ).containsExactlyInAnyOrder( "child_a", "sub_child_a" );
103+
inspector.assertNumberOfJoins( 0, 1 );
104+
inspector.clear();
105+
106+
assertThat( session.createQuery(
107+
"select p.name from ParentEntity p where treat(p as ChildB).id is not null",
108+
String.class
109+
).getSingleResult() ).isEqualTo( "child_b" );
110+
inspector.assertNumberOfJoins( 0, 1 );
111+
inspector.clear();
94112
} );
95113
}
96114

@@ -121,8 +139,9 @@ public void testSelectInstance(SessionFactoryScope scope) {
121139
inspector.clear();
122140

123141
scope.inTransaction( session -> {
124-
// NOTE: we currently always join all subclasses when selecting the entity instance. We could
125-
// maybe avoid this when we have a physical discriminator column and a type filter
142+
// With type filters we still join all subclasses when selecting the entity instance
143+
// because we are not aware of the type restriction when processing the selection
144+
126145
assertThat( session.createQuery(
127146
"from ParentEntity p where type(p) = ParentEntity",
128147
ParentEntity.class
@@ -142,6 +161,22 @@ public void testSelectInstance(SessionFactoryScope scope) {
142161
ParentEntity.class
143162
).getResultList() ).hasSize( 1 );
144163
inspector.assertNumberOfJoins( 0, 3 );
164+
inspector.clear();
165+
166+
// With treat() we only join the needed subclasses
167+
168+
assertThat( session.createQuery(
169+
"select treat(p as ChildA) from ParentEntity p",
170+
ParentEntity.class
171+
).getResultList() ).hasSize( 2 );
172+
inspector.assertNumberOfJoins( 0, 2 );
173+
inspector.clear();
174+
175+
assertThat( session.createQuery(
176+
"select treat(p as ChildB) from ParentEntity p",
177+
ParentEntity.class
178+
).getResultList() ).hasSize( 1 );
179+
inspector.assertNumberOfJoins( 0, 1 );
145180
} );
146181
}
147182

0 commit comments

Comments
 (0)