HHH-14156 IN subquery predicate with entity aliases produces wrong SQL "too few columns in subquery"

This commit is contained in:
Nathan Xu 2020-08-17 10:06:30 -04:00 committed by Christian Beikov
parent 00d9c12f05
commit be64851fee
2 changed files with 82 additions and 0 deletions

View File

@ -214,6 +214,7 @@ public class IdentNode extends FromReferenceNode implements SelectExpression {
final boolean shouldSkipWrappingInParenthesis =
(isInDistinctCount && ! dialect.requiresParensForTupleDistinctCounts())
|| isInNonDistinctCount
|| getWalker().isInSelect() && !getWalker().isInCase() && !isInCount // HHH-14156
|| getWalker().getCurrentTopLevelClauseType() == HqlSqlTokenTypes.ORDER
|| getWalker().getCurrentTopLevelClauseType() == HqlSqlTokenTypes.GROUP;
if ( ! shouldSkipWrappingInParenthesis ) {

View File

@ -0,0 +1,81 @@
package org.hibernate.query.hhh14156;
import java.io.Serializable;
import java.util.Objects;
import javax.persistence.Embeddable;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Table;
import org.hibernate.dialect.PostgreSQL82Dialect;
import org.hibernate.testing.RequiresDialect;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
/**
* @author Nathan Xu
* @author Christian Beikov
*/
@TestForIssue( jiraKey = "HHH-14156" )
@RequiresDialect( PostgreSQL82Dialect.class )
public class HHH14156Test extends BaseCoreFunctionalTestCase {
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] { EntityWithCompositeId.class };
}
@Test
public void testNoExceptionThrown() {
inTransaction( session ->
session.createQuery(
"from EntityWithCompositeId e where e in (select e2 from EntityWithCompositeId e2)",
EntityWithCompositeId.class
).getResultList()
);
}
@Entity(name = "EntityWithCompositeId")
@Table(name = "EntityWithCompositeId")
public static class EntityWithCompositeId implements Serializable {
@EmbeddedId
private PK id;
@Embeddable
public static class PK implements Serializable {
private String id1;
private String id2;
public PK(String id1, String id2) {
this.id1 = id1;
this.id2 = id2;
}
private PK() {
}
@Override
public boolean equals(Object o) {
if ( this == o ) {
return true;
}
if ( o == null || getClass() != o.getClass() ) {
return false;
}
PK pk = (PK) o;
return Objects.equals( id1, pk.id1 ) &&
Objects.equals( id2, pk.id2 );
}
@Override
public int hashCode() {
return Objects.hash( id1, id2 );
}
}
}
}