Skip to content

Commit ce32b36

Browse files
beikovsebersole
authored andcommitted
HHH-11538 - Testcase that asserts only a single join is generated
1 parent d59ac45 commit ce32b36

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

hibernate-core/src/test/java/org/hibernate/test/hql/EntityJoinTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77
package org.hibernate.test.hql;
88

9+
import java.util.Collections;
910
import java.util.List;
1011
import javax.persistence.Entity;
1112
import javax.persistence.FetchType;
@@ -15,6 +16,9 @@
1516
import javax.persistence.Table;
1617

1718
import org.hibernate.annotations.NaturalId;
19+
import org.hibernate.engine.query.spi.HQLQueryPlan;
20+
import org.hibernate.hql.spi.QueryTranslator;
21+
1822
import org.hibernate.testing.TestForIssue;
1923
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
2024
import org.junit.After;
@@ -23,11 +27,14 @@
2327

2428
import static org.hamcrest.core.Is.is;
2529
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
30+
import static org.junit.Assert.assertEquals;
31+
import static org.junit.Assert.assertNotEquals;
2632
import static org.junit.Assert.assertNull;
2733
import static org.junit.Assert.assertThat;
2834

2935
/**
3036
* @author Steve Ebersole, Jan Martiska
37+
* @author Christian Beikov
3138
*/
3239
public class EntityJoinTest extends BaseNonConfigCoreFunctionalTestCase {
3340
@Override
@@ -143,6 +150,29 @@ public void testJoinOnEntityJoinNode() {
143150
} );
144151
}
145152

153+
@Test
154+
@TestForIssue(jiraKey = "HHH-11538")
155+
public void testNoImpliedJoinGeneratedForEqualityComparison() {
156+
doInHibernate( this::sessionFactory, session -> {
157+
final HQLQueryPlan plan = sessionFactory().getQueryPlanCache().getHQLQueryPlan(
158+
"select r.id, cust.name " +
159+
"from FinancialRecord r " +
160+
" join Customer cust on r.customer = cust" +
161+
" order by r.id",
162+
false,
163+
Collections.EMPTY_MAP
164+
);
165+
assertEquals( 1, plan.getTranslators().length );
166+
final QueryTranslator translator = plan.getTranslators()[0];
167+
final String generatedSql = translator.getSQLString();
168+
169+
int tableReferenceIndex = generatedSql.indexOf( " customer " );
170+
assertNotEquals("Generated SQL doesn't contain a table reference for customer", -1, tableReferenceIndex );
171+
int nextTableReferenceIndex = generatedSql.indexOf( " customer ", tableReferenceIndex + 1 );
172+
assertEquals("Generated SQL wrongly joined customer twice", -1, nextTableReferenceIndex );
173+
} );
174+
}
175+
146176
@Test
147177
public void testRightOuterEntityJoins() {
148178
doInHibernate( this::sessionFactory, session -> {

0 commit comments

Comments
 (0)