Skip to content

Commit

Permalink
HHH-12492 Qualify references to columns from the target table in subq…
Browse files Browse the repository at this point in the history
…ueries in DELETE/UPDATE queries

Don't try to duplicate the logic from
org.hibernate.hql.internal.ast.tree.FromElementType#toColumns(java.lang.String, java.lang.String, boolean, boolean)
in other classes, it's complex enough and already seems to handle all
the cases we might encounter.

In this specific case, we want the table name to be used to qualify
column names, because the target table doesn't have any alias (it's not
supported by every version of every RDBMS), and not qualifying columns
at all may lead to a confusing statement, in particular if tables
referenced in the subquery contain columns with the same name.
Since we use aliases for every other table in the query, referencing the
table should not lead to any conflict.
  • Loading branch information
yrodiere authored and gsmet committed Jul 27, 2018
1 parent 48f59e8 commit dc42dea
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -341,12 +341,10 @@ public String[] getIdentityColumns() {

final String propertyName = getIdentifierPropertyName();

if ( getWalker().getStatementType() == HqlSqlTokenTypes.SELECT ) {
return getPropertyMapping( propertyName ).toColumns( table, propertyName );
}
else {
return getPropertyMapping( propertyName ).toColumns( propertyName );
}
return toColumns(
table, propertyName,
getWalker().getStatementType() == HqlSqlTokenTypes.SELECT
);
}

public void setCollectionJoin(boolean collectionJoin) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,6 @@ private boolean resolveAsAlias() {

String[] columnExpressions = element.getIdentityColumns();

// determine whether to apply qualification (table alias) to the column(s)...
if ( ! isFromElementUpdateOrDeleteRoot( element ) ) {
if ( StringHelper.isNotEmpty( element.getTableAlias() ) ) {
// apparently we also need to check that they are not already qualified. Ugh!
columnExpressions = StringHelper.qualifyIfNot( element.getTableAlias(), columnExpressions );
}
}

final Dialect dialect = getWalker().getSessionFactoryHelper().getFactory().getDialect();
final boolean isInCount = getWalker().isInCount();
final boolean isInDistinctCount = isInCount && getWalker().isInCountDistinct();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;

import org.hibernate.testing.FailureExpected;
import org.hibernate.testing.TestForIssue;
import org.junit.Assert;
import org.junit.Test;
Expand All @@ -26,7 +25,6 @@ protected Class<?>[] getAnnotatedClasses() {
}

@Test
@FailureExpected(jiraKey = "HHH-12492")
public void testSubQueryReferencingTargetProperty() {
// prepare
doInJPA( this::entityManagerFactory, entityManager -> {
Expand Down

0 comments on commit dc42dea

Please sign in to comment.