HHH-8551 Cannot use with-clause on the RHS of a join

This commit is contained in:
Brett Meyer 2013-09-24 17:09:32 -04:00
parent 0062600296
commit 1a621666e1
3 changed files with 55 additions and 22 deletions

View File

@ -209,10 +209,8 @@ public class JoinSequence {
} }
if ( withClauseFragment != null ) { if ( withClauseFragment != null ) {
if ( join.getAlias().equals( withClauseJoinAlias ) ) {
condition += " and " + withClauseFragment; condition += " and " + withClauseFragment;
} }
}
joinFragment.addJoin( joinFragment.addJoin(
join.getJoinable().getTableName(), join.getJoinable().getTableName(),

View File

@ -421,10 +421,8 @@ public class HqlSqlWalker extends HqlSqlBaseWalker implements ErrorReporter, Par
else { else {
FromElement referencedFromElement = visitor.getReferencedFromElement(); FromElement referencedFromElement = visitor.getReferencedFromElement();
if ( referencedFromElement != fromElement ) { if ( referencedFromElement != fromElement ) {
throw new InvalidWithClauseException( LOG.warn( "with-clause expressions do not reference the from-clause element to which the with-clause was associated. The query may not work as expected..."
"with-clause expressions did not reference from-clause element to which the with-clause was associated", + queryTranslatorImpl.getQueryString() );
queryTranslatorImpl.getQueryString()
);
} }
} }

View File

@ -22,19 +22,23 @@
* Boston, MA 02110-1301 USA * Boston, MA 02110-1301 USA
*/ */
package org.hibernate.test.hql; package org.hibernate.test.hql;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.junit.Test;
import org.hibernate.HibernateException; import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session; import org.hibernate.Session;
import org.hibernate.Transaction; import org.hibernate.Transaction;
import org.hibernate.hql.internal.ast.InvalidWithClauseException; import org.hibernate.hql.internal.ast.InvalidWithClauseException;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/** /**
* Implementation of WithClauseTest. * Implementation of WithClauseTest.
@ -43,7 +47,7 @@ import static org.junit.Assert.fail;
*/ */
public class WithClauseTest extends BaseCoreFunctionalTestCase { public class WithClauseTest extends BaseCoreFunctionalTestCase {
public String[] getMappings() { public String[] getMappings() {
return new String[] { "hql/Animal.hbm.xml" }; return new String[] { "hql/Animal.hbm.xml", "hql/SimpleEntityWithAssociation.hbm.xml" };
} }
@Test @Test
@ -88,14 +92,6 @@ public class WithClauseTest extends BaseCoreFunctionalTestCase {
catch( InvalidWithClauseException expected ) { catch( InvalidWithClauseException expected ) {
} }
try {
s.createQuery( "from Animal a inner join a.offspring o inner join o.mother as m inner join m.father as f with o.bodyWeight > 1" )
.list();
fail( "failure expected" );
}
catch( InvalidWithClauseException expected ) {
}
try { try {
s.createQuery( "from Human h inner join h.offspring o with o.mother.father = :cousin" ) s.createQuery( "from Human h inner join h.offspring o with o.mother.father = :cousin" )
.setEntity( "cousin", s.load( Human.class, new Long(123) ) ) .setEntity( "cousin", s.load( Human.class, new Long(123) ) )
@ -145,6 +141,47 @@ public class WithClauseTest extends BaseCoreFunctionalTestCase {
data.cleanup(); data.cleanup();
} }
@Test
@TestForIssue(jiraKey = "HHH-2772")
public void testWithJoinRHS() {
Session s = openSession();
s.beginTransaction();
SimpleEntityWithAssociation entity1 = new SimpleEntityWithAssociation();
entity1.setName( "entity1" );
SimpleEntityWithAssociation entity2 = new SimpleEntityWithAssociation();
entity2.setName( "entity2" );
SimpleAssociatedEntity associatedEntity1 = new SimpleAssociatedEntity();
associatedEntity1.setName( "associatedEntity1" );
SimpleAssociatedEntity associatedEntity2 = new SimpleAssociatedEntity();
associatedEntity2.setName( "associatedEntity2" );
entity1.addAssociation( associatedEntity1 );
entity2.addAssociation( associatedEntity2 );
s.persist( entity1 );
s.persist( entity2 );
s.getTransaction().commit();
s.clear();
s.beginTransaction();
Query query = s.createQuery( "select a from SimpleEntityWithAssociation as e INNER JOIN e.associatedEntities as a WITH e.name=?" );
query.setParameter( 0, "entity1" );
List list = query.list();
assertEquals( list.size(), 1 );
SimpleAssociatedEntity associatedEntity = (SimpleAssociatedEntity) query.list().get( 0 );
assertNotNull( associatedEntity );
assertEquals( associatedEntity.getName(), "associatedEntity1" );
assertEquals( associatedEntity.getOwner().getName(), "entity1" );
s.getTransaction().commit();
s.close();
}
private class TestData { private class TestData {
public void prepare() { public void prepare() {
Session session = openSession(); Session session = openSession();