HHH-18378 Add test for issue

This commit is contained in:
Marco Belladelli 2024-07-29 15:11:09 +02:00
parent 171488a762
commit 0272fc7fbd
1 changed files with 23 additions and 9 deletions

View File

@ -44,6 +44,7 @@ import static org.hibernate.jpa.SpecHints.HINT_SPEC_FETCH_GRAPH;
} ) } )
@SessionFactory( useCollectingStatementInspector = true ) @SessionFactory( useCollectingStatementInspector = true )
@Jira( "https://hibernate.atlassian.net/browse/HHH-17629" ) @Jira( "https://hibernate.atlassian.net/browse/HHH-17629" )
@Jira( "https://hibernate.atlassian.net/browse/HHH-18378" )
public class EntityGraphAndJoinTest { public class EntityGraphAndJoinTest {
@BeforeAll @BeforeAll
public void setUp(SessionFactoryScope scope) { public void setUp(SessionFactoryScope scope) {
@ -59,25 +60,35 @@ public class EntityGraphAndJoinTest {
@Test @Test
public void testHqlJoin(SessionFactoryScope scope) { public void testHqlJoin(SessionFactoryScope scope) {
executeQuery( scope, false, false ); executeQuery( scope, false, false, false );
} }
@Test @Test
public void testHqlLeftJoin(SessionFactoryScope scope) { public void testHqlLeftJoin(SessionFactoryScope scope) {
executeQuery( scope, false, true ); executeQuery( scope, false, true, false );
} }
@Test @Test
public void testCriteriaJoin(SessionFactoryScope scope) { public void testCriteriaJoin(SessionFactoryScope scope) {
executeQuery( scope, true, false ); executeQuery( scope, true, false, false );
} }
@Test @Test
public void testCriteriaLeftJoin(SessionFactoryScope scope) { public void testCriteriaLeftJoin(SessionFactoryScope scope) {
executeQuery( scope, true, true ); executeQuery( scope, true, true, false );
} }
private void executeQuery(SessionFactoryScope scope, boolean criteria, boolean leftJoin) { @Test
public void testHqlJoinWhere(SessionFactoryScope scope) {
executeQuery( scope, false, false, true );
}
@Test
public void testCriteriaLeftJoinWhere(SessionFactoryScope scope) {
executeQuery( scope, true, true, true );
}
private void executeQuery(SessionFactoryScope scope, boolean criteria, boolean leftJoin, boolean where) {
final SQLStatementInspector inspector = scope.getCollectingStatementInspector(); final SQLStatementInspector inspector = scope.getCollectingStatementInspector();
inspector.clear(); inspector.clear();
@ -88,13 +99,16 @@ public class EntityGraphAndJoinTest {
final CriteriaQuery<Person> cq = cb.createQuery( Person.class ); final CriteriaQuery<Person> cq = cb.createQuery( Person.class );
final Root<Person> root = cq.from( Person.class ); final Root<Person> root = cq.from( Person.class );
final Join<Person, Address> join = root.join( "address", leftJoin ? JoinType.LEFT : JoinType.INNER ); final Join<Person, Address> join = root.join( "address", leftJoin ? JoinType.LEFT : JoinType.INNER );
cq.distinct( true ).where( cb.equal( join.get( "description" ), "test" ) ).orderBy( cb.asc( join.get( "id" ) ) ); if ( where ) {
cq.where( cb.equal( join.get( "description" ), "test" ) );
}
query = session.createQuery( cq.distinct( true ) ); query = session.createQuery( cq.distinct( true ) );
} }
else { else {
query = session.createQuery( String.format( query = session.createQuery( String.format(
"select distinct p from Person p %s p.address a where a.description = 'test' order by a.id", "select distinct p from Person p %s p.address a %s",
leftJoin ? "left join" : "join" leftJoin ? "left join" : "join",
where ? "where a.description = 'test'" : ""
), Person.class ); ), Person.class );
} }
final EntityGraph<?> entityGraph = session.getEntityGraph( "test-graph" ); final EntityGraph<?> entityGraph = session.getEntityGraph( "test-graph" );
@ -102,7 +116,7 @@ public class EntityGraphAndJoinTest {
assertThat( resultList ).hasSize( 2 ); assertThat( resultList ).hasSize( 2 );
assertThat( resultList.stream().map( p -> p.getAddress().getId() ) ).containsExactly( 1L, 2L ); assertThat( resultList.stream().map( p -> p.getAddress().getId() ) ).containsExactly( 1L, 2L );
inspector.assertExecutedCount( 1 ); inspector.assertExecutedCount( 1 );
inspector.assertNumberOfOccurrenceInQuery( 0, "join", 1 ); inspector.assertNumberOfOccurrenceInQuery( 0, "join", where ? 2 : 1 );
} ); } );
} }