Skip to content

Commit

Permalink
HHH-8551 Cannot use with-clause on the RHS of a join
Browse files Browse the repository at this point in the history
  • Loading branch information
brmeyer committed Sep 25, 2013
1 parent 0062600 commit 1a62166
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 22 deletions.
Expand Up @@ -50,7 +50,7 @@
* @see JoinFragment
*/
public class JoinSequence {
private final SessionFactoryImplementor factory;
private final SessionFactoryImplementor factory;

private final StringBuilder conditions = new StringBuilder();
private final List<Join> joins = new ArrayList<Join>();
Expand Down Expand Up @@ -209,9 +209,7 @@ && isManyToManyRoot( last )
}

if ( withClauseFragment != null ) {
if ( join.getAlias().equals( withClauseJoinAlias ) ) {
condition += " and " + withClauseFragment;
}
condition += " and " + withClauseFragment;
}

joinFragment.addJoin(
Expand Down
Expand Up @@ -421,10 +421,8 @@ private void handleWithFragment(FromElement fromElement, AST hqlWithNode) throws
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",
queryTranslatorImpl.getQueryString()
);
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() );
}
}

Expand Down
Expand Up @@ -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.
Expand All @@ -43,7 +47,7 @@
*/
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
Expand Down Expand Up @@ -88,14 +92,6 @@ public void testInvalidWithSemantics() {
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) ) )
Expand Down Expand Up @@ -144,6 +140,47 @@ public void testWithClause() {

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() {
Expand Down

0 comments on commit 1a62166

Please sign in to comment.