From d6ecc37c0ffe655e598cfb6c692ed7f053a58ba4 Mon Sep 17 00:00:00 2001 From: stasal Date: Wed, 14 Dec 2016 14:50:44 +0700 Subject: [PATCH] HHH-11337 - Incorrect SQL generated when use both left join with unrelated entity and implicit join to another entity in select-clause (cherry picked from commit b2df137ed6ea113176163d702e7bd0b0f0ea3780) --- .../hql/internal/ast/SqlGenerator.java | 4 +-- .../hibernate/test/hql/EntityJoinTest.java | 32 +++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/hql/internal/ast/SqlGenerator.java b/hibernate-core/src/main/java/org/hibernate/hql/internal/ast/SqlGenerator.java index ec1accddad..01ea8d5775 100644 --- a/hibernate-core/src/main/java/org/hibernate/hql/internal/ast/SqlGenerator.java +++ b/hibernate-core/src/main/java/org/hibernate/hql/internal/ast/SqlGenerator.java @@ -397,7 +397,7 @@ public class SqlGenerator extends SqlGeneratorBase implements ErrorReporter { if ( right.getRealOrigin() == left ) { // right represents a joins originating from left... if ( right.getJoinSequence() != null && right.getJoinSequence().isThetaStyle() ) { - out( ", " ); + writeCrossJoinSeparator(); } else { out( " " ); @@ -406,7 +406,7 @@ public class SqlGenerator extends SqlGeneratorBase implements ErrorReporter { else { // not so sure this is even valid subtree. but if it was, it'd // represent two unrelated table references... - out( ", " ); + writeCrossJoinSeparator(); } } out( d ); diff --git a/hibernate-core/src/test/java/org/hibernate/test/hql/EntityJoinTest.java b/hibernate-core/src/test/java/org/hibernate/test/hql/EntityJoinTest.java index b7bde19d5e..7865eef6bc 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/hql/EntityJoinTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/hql/EntityJoinTest.java @@ -15,6 +15,7 @@ import javax.persistence.Table; import org.hibernate.Session; import org.hibernate.annotations.NaturalId; +import org.hibernate.testing.TestForIssue; import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; import org.junit.After; import org.junit.Before; @@ -108,6 +109,37 @@ public class EntityJoinTest extends BaseNonConfigCoreFunctionalTestCase { } } + @Test + @TestForIssue(jiraKey = "HHH-11337") + @SuppressWarnings("unchecked") + public void testLeftOuterEntityJoinsWithImplicitInnerJoinInSelectClause() { + Session session = openSession(); + session.beginTransaction(); + + try { + // this should get all financial records even if their lastUpdateBy user is null + List result = session.createQuery( + "select r.id, u.id, u.username, r.customer.name " + + "from FinancialRecord r " + + " left join User u on r.lastUpdateBy = u.username" + + " order by r.id" + ).list(); + assertThat(result.size(), is(2)); + + Object[] stevesRecord = result.get(0); + assertThat(stevesRecord[0], is(1)); + assertThat(stevesRecord[2], is("steve")); + + Object[] noOnesRecord = result.get(1); + assertThat(noOnesRecord[0], is(2)); + assertNull(noOnesRecord[2]); + + } finally { + session.getTransaction().commit(); + session.close(); + } + } + @Test @SuppressWarnings("unchecked") public void testRightOuterEntityJoins() {