44
44
} )
45
45
@ SessionFactory ( useCollectingStatementInspector = true )
46
46
@ Jira ( "https://hibernate.atlassian.net/browse/HHH-17629" )
47
+ @ Jira ( "https://hibernate.atlassian.net/browse/HHH-18378" )
47
48
public class EntityGraphAndJoinTest {
48
49
@ BeforeAll
49
50
public void setUp (SessionFactoryScope scope ) {
@@ -59,25 +60,35 @@ public void setUp(SessionFactoryScope scope) {
59
60
60
61
@ Test
61
62
public void testHqlJoin (SessionFactoryScope scope ) {
62
- executeQuery ( scope , false , false );
63
+ executeQuery ( scope , false , false , false );
63
64
}
64
65
65
66
@ Test
66
67
public void testHqlLeftJoin (SessionFactoryScope scope ) {
67
- executeQuery ( scope , false , true );
68
+ executeQuery ( scope , false , true , false );
68
69
}
69
70
70
71
@ Test
71
72
public void testCriteriaJoin (SessionFactoryScope scope ) {
72
- executeQuery ( scope , true , false );
73
+ executeQuery ( scope , true , false , false );
73
74
}
74
75
75
76
@ Test
76
77
public void testCriteriaLeftJoin (SessionFactoryScope scope ) {
77
- executeQuery ( scope , true , true );
78
+ executeQuery ( scope , true , true , false );
78
79
}
79
80
80
- private void executeQuery (SessionFactoryScope scope , boolean criteria , boolean leftJoin ) {
81
+ @ Test
82
+ public void testHqlJoinWhere (SessionFactoryScope scope ) {
83
+ executeQuery ( scope , false , false , true );
84
+ }
85
+
86
+ @ Test
87
+ public void testCriteriaLeftJoinWhere (SessionFactoryScope scope ) {
88
+ executeQuery ( scope , true , true , true );
89
+ }
90
+
91
+ private void executeQuery (SessionFactoryScope scope , boolean criteria , boolean leftJoin , boolean where ) {
81
92
final SQLStatementInspector inspector = scope .getCollectingStatementInspector ();
82
93
inspector .clear ();
83
94
@@ -88,21 +99,24 @@ private void executeQuery(SessionFactoryScope scope, boolean criteria, boolean l
88
99
final CriteriaQuery <Person > cq = cb .createQuery ( Person .class );
89
100
final Root <Person > root = cq .from ( Person .class );
90
101
final Join <Person , Address > join = root .join ( "address" , leftJoin ? JoinType .LEFT : JoinType .INNER );
91
- cq .distinct ( true ).where ( cb .equal ( join .get ( "description" ), "test" ) ).orderBy ( cb .asc ( join .get ( "id" ) ) );
102
+ if ( where ) {
103
+ cq .where ( cb .equal ( join .get ( "description" ), "test" ) );
104
+ }
92
105
query = session .createQuery ( cq .distinct ( true ) );
93
106
}
94
107
else {
95
108
query = session .createQuery ( String .format (
96
- "select distinct p from Person p %s p.address a where a.description = 'test' order by a.id" ,
97
- leftJoin ? "left join" : "join"
109
+ "select distinct p from Person p %s p.address a %s" ,
110
+ leftJoin ? "left join" : "join" ,
111
+ where ? "where a.description = 'test'" : ""
98
112
), Person .class );
99
113
}
100
114
final EntityGraph <?> entityGraph = session .getEntityGraph ( "test-graph" );
101
115
final List <Person > resultList = query .setHint ( HINT_SPEC_FETCH_GRAPH , entityGraph ).getResultList ();
102
116
assertThat ( resultList ).hasSize ( 2 );
103
117
assertThat ( resultList .stream ().map ( p -> p .getAddress ().getId () ) ).containsExactly ( 1L , 2L );
104
118
inspector .assertExecutedCount ( 1 );
105
- inspector .assertNumberOfOccurrenceInQuery ( 0 , "join" , 1 );
119
+ inspector .assertNumberOfOccurrenceInQuery ( 0 , "join" , where ? 2 : 1 );
106
120
} );
107
121
}
108
122
0 commit comments