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

Conflicts:
	hibernate-core/src/main/java/org/hibernate/engine/internal/JoinSequence.java
	hibernate-core/src/main/java/org/hibernate/hql/internal/ast/HqlSqlWalker.java
This commit is contained in:
Brett Meyer 2013-09-25 16:26:47 -04:00
parent 17384d6d02
commit ac55afbd65
3 changed files with 54 additions and 18 deletions

View File

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

View File

@ -421,7 +421,8 @@ public class HqlSqlWalker extends HqlSqlBaseWalker implements ErrorReporter, Par
else {
FromElement referencedFromElement = visitor.getReferencedFromElement();
if ( referencedFromElement != fromElement ) {
throw new InvalidWithClauseException( "with-clause expressions did not reference from-clause element to which the with-clause was associated" );
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..."
+ queryTranslatorImpl.getQueryString() );
}
}

View File

@ -22,19 +22,23 @@
* Boston, MA 02110-1301 USA
*/
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.List;
import org.junit.Test;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.hql.internal.ast.InvalidWithClauseException;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.junit.Test;
/**
* Implementation of WithClauseTest.
@ -43,7 +47,7 @@ import static org.junit.Assert.fail;
*/
public class WithClauseTest extends BaseCoreFunctionalTestCase {
public String[] getMappings() {
return new String[] { "hql/Animal.hbm.xml" };
return new String[] { "hql/Animal.hbm.xml", "hql/SimpleEntityWithAssociation.hbm.xml" };
}
@Test
@ -88,14 +92,6 @@ public class WithClauseTest extends BaseCoreFunctionalTestCase {
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 {
s.createQuery( "from Human h inner join h.offspring o with o.mother.father = :cousin" )
.setEntity( "cousin", s.load( Human.class, new Long(123) ) )
@ -144,6 +140,47 @@ public class WithClauseTest extends BaseCoreFunctionalTestCase {
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 {
public void prepare() {