diff --git a/hibernate-core/src/main/java/org/hibernate/hql/internal/ast/tree/IdentNode.java b/hibernate-core/src/main/java/org/hibernate/hql/internal/ast/tree/IdentNode.java index 1574c57feb..26e5d0ee4d 100644 --- a/hibernate-core/src/main/java/org/hibernate/hql/internal/ast/tree/IdentNode.java +++ b/hibernate-core/src/main/java/org/hibernate/hql/internal/ast/tree/IdentNode.java @@ -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 ) { diff --git a/hibernate-core/src/test/java/org/hibernate/query/hhh14156/HHH14156Test.java b/hibernate-core/src/test/java/org/hibernate/query/hhh14156/HHH14156Test.java new file mode 100644 index 0000000000..795844c062 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/query/hhh14156/HHH14156Test.java @@ -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 ); + } + } + } +}