4444} )
4545@ SessionFactory ( useCollectingStatementInspector = true )
4646@ Jira ( "https://hibernate.atlassian.net/browse/HHH-17629" )
47+ @ Jira ( "https://hibernate.atlassian.net/browse/HHH-18378" )
4748public class EntityGraphAndJoinTest {
4849 @ BeforeAll
4950 public void setUp (SessionFactoryScope scope ) {
@@ -59,25 +60,35 @@ public void setUp(SessionFactoryScope scope) {
5960
6061 @ Test
6162 public void testHqlJoin (SessionFactoryScope scope ) {
62- executeQuery ( scope , false , false );
63+ executeQuery ( scope , false , false , false );
6364 }
6465
6566 @ Test
6667 public void testHqlLeftJoin (SessionFactoryScope scope ) {
67- executeQuery ( scope , false , true );
68+ executeQuery ( scope , false , true , false );
6869 }
6970
7071 @ Test
7172 public void testCriteriaJoin (SessionFactoryScope scope ) {
72- executeQuery ( scope , true , false );
73+ executeQuery ( scope , true , false , false );
7374 }
7475
7576 @ Test
7677 public void testCriteriaLeftJoin (SessionFactoryScope scope ) {
77- executeQuery ( scope , true , true );
78+ executeQuery ( scope , true , true , false );
7879 }
7980
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 ) {
8192 final SQLStatementInspector inspector = scope .getCollectingStatementInspector ();
8293 inspector .clear ();
8394
@@ -88,21 +99,24 @@ private void executeQuery(SessionFactoryScope scope, boolean criteria, boolean l
8899 final CriteriaQuery <Person > cq = cb .createQuery ( Person .class );
89100 final Root <Person > root = cq .from ( Person .class );
90101 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+ }
92105 query = session .createQuery ( cq .distinct ( true ) );
93106 }
94107 else {
95108 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'" : ""
98112 ), Person .class );
99113 }
100114 final EntityGraph <?> entityGraph = session .getEntityGraph ( "test-graph" );
101115 final List <Person > resultList = query .setHint ( HINT_SPEC_FETCH_GRAPH , entityGraph ).getResultList ();
102116 assertThat ( resultList ).hasSize ( 2 );
103117 assertThat ( resultList .stream ().map ( p -> p .getAddress ().getId () ) ).containsExactly ( 1L , 2L );
104118 inspector .assertExecutedCount ( 1 );
105- inspector .assertNumberOfOccurrenceInQuery ( 0 , "join" , 1 );
119+ inspector .assertNumberOfOccurrenceInQuery ( 0 , "join" , where ? 2 : 1 );
106120 } );
107121 }
108122
0 commit comments