Skip to content

Commit

Permalink
HHH-1930 - QuerySyntaxException "with-clause expressions did not refe…
Browse files Browse the repository at this point in the history
…rence from-clause element to which the with-clause was associated"

git-svn-id: https://svn.jboss.org/repos/hibernate/core/branches/Branch_3_2@16667 1b8cb986-b30d-0410-93ca-fae66ebed9b2
  • Loading branch information
sebersole committed Jun 2, 2009
1 parent 3df61e2 commit 5984726
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
34 changes: 20 additions & 14 deletions src/org/hibernate/hql/ast/HqlSqlWalker.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Arrays;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand Down Expand Up @@ -43,7 +44,6 @@
import org.hibernate.hql.ast.tree.SelectClause;
import org.hibernate.hql.ast.tree.SelectExpression;
import org.hibernate.hql.ast.tree.UpdateStatement;
import org.hibernate.hql.ast.tree.Node;
import org.hibernate.hql.ast.tree.OperatorNode;
import org.hibernate.hql.ast.tree.ParameterContainer;
import org.hibernate.hql.ast.util.ASTPrinter;
Expand Down Expand Up @@ -361,8 +361,7 @@ protected void createFromJoinElement(
}
}

private void handleWithFragment(FromElement fromElement, AST hqlWithNode) throws SemanticException
{
private void handleWithFragment(FromElement fromElement, AST hqlWithNode) throws SemanticException {
try {
withClause( hqlWithNode );
AST hqlSqlWithNode = returnAST;
Expand All @@ -372,13 +371,22 @@ private void handleWithFragment(FromElement fromElement, AST hqlWithNode) throws
WithClauseVisitor visitor = new WithClauseVisitor( fromElement );
NodeTraverser traverser = new NodeTraverser( visitor );
traverser.traverseDepthFirst( hqlSqlWithNode );
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" );

String withClauseJoinAlias = visitor.getJoinAlias();
if ( withClauseJoinAlias == null ) {
withClauseJoinAlias = fromElement.getCollectionTableAlias();
}
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" );
}
}

SqlGenerator sql = new SqlGenerator( getSessionFactoryHelper().getFactory() );
sql.whereExpr( hqlSqlWithNode.getFirstChild() );
fromElement.setWithClauseFragment( visitor.getJoinAlias(), "(" + sql.getSQL() + ")" );

fromElement.setWithClauseFragment( withClauseJoinAlias, "(" + sql.getSQL() + ")" );
}
catch( SemanticException e ) {
throw e;
Expand Down Expand Up @@ -866,8 +874,8 @@ protected AST generatePositionalParameter(AST inputNode) throws SemanticExceptio
}
ParameterNode parameter = ( ParameterNode ) astFactory.create( PARAM, "?" );
PositionalParameterSpecification paramSpec = new PositionalParameterSpecification(
( ( Node ) inputNode ).getLine(),
( ( Node ) inputNode ).getColumn(),
inputNode.getLine(),
inputNode.getColumn(),
positionalParameterCount++
);
parameter.setHqlParameterSpecification( paramSpec );
Expand All @@ -885,8 +893,8 @@ protected AST generateNamedParameter(AST delimiterNode, AST nameNode) throws Sem
parameter.setText( "?" );

NamedParameterSpecification paramSpec = new NamedParameterSpecification(
( ( Node ) delimiterNode ).getLine(),
( ( Node ) delimiterNode ).getColumn(),
delimiterNode.getLine(),
delimiterNode.getColumn(),
name
);
parameter.setHqlParameterSpecification( paramSpec );
Expand Down Expand Up @@ -961,9 +969,7 @@ public int[] getNamedParameterLocations(String name) throws QueryException {
}

public void addQuerySpaces(Serializable[] spaces) {
for ( int i = 0; i < spaces.length; i++ ) {
querySpaces.add( spaces[i] );
}
querySpaces.addAll( Arrays.asList( spaces ) );
}

public Type[] getReturnTypes() {
Expand Down
5 changes: 5 additions & 0 deletions test/org/hibernate/test/hql/WithClauseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ public void testWithClause() {
.list();
assertTrue( "ad-hoc on did not take effect", list.isEmpty() );

// http://opensource.atlassian.com/projects/hibernate/browse/HHH-1930
list = s.createQuery( "from Human h inner join h.nickNames as nicknames with nicknames = 'abc'" )
.list();
assertTrue( "ad-hoc on did not take effect", list.isEmpty() );

txn.commit();
s.close();

Expand Down

0 comments on commit 5984726

Please sign in to comment.