From 90be61210c189dc7799f81a7a3a4dbea57280174 Mon Sep 17 00:00:00 2001 From: Nathan Xu Date: Tue, 25 Aug 2020 10:16:12 -0400 Subject: [PATCH] HHH-11877 wrap CompoundPredicate's expression list --- .../internal/predicate/CompoundPredicate.java | 5 ++- .../query/criteria/internal/hhh11877/Foo.java | 31 +++++++++++++ .../internal/hhh11877/HHH111877Test.java | 44 +++++++++++++++++++ 3 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 hibernate-core/src/test/java/org/hibernate/query/criteria/internal/hhh11877/Foo.java create mode 100644 hibernate-core/src/test/java/org/hibernate/query/criteria/internal/hhh11877/HHH111877Test.java diff --git a/hibernate-core/src/main/java/org/hibernate/query/criteria/internal/predicate/CompoundPredicate.java b/hibernate-core/src/main/java/org/hibernate/query/criteria/internal/predicate/CompoundPredicate.java index 00840a7c47..212680ca95 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/criteria/internal/predicate/CompoundPredicate.java +++ b/hibernate-core/src/main/java/org/hibernate/query/criteria/internal/predicate/CompoundPredicate.java @@ -80,7 +80,10 @@ public class CompoundPredicate private void applyExpressions(List> expressions) { this.expressions.clear(); - this.expressions.addAll( expressions ); + final CriteriaBuilderImpl criteriaBuilder = criteriaBuilder(); + for ( Expression expression : expressions ) { + this.expressions.add( criteriaBuilder.wrap( expression ) ); + } } @Override diff --git a/hibernate-core/src/test/java/org/hibernate/query/criteria/internal/hhh11877/Foo.java b/hibernate-core/src/test/java/org/hibernate/query/criteria/internal/hhh11877/Foo.java new file mode 100644 index 0000000000..efaeff9231 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/query/criteria/internal/hhh11877/Foo.java @@ -0,0 +1,31 @@ +package org.hibernate.query.criteria.internal.hhh11877; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Entity +public class Foo { + + private long id; + private boolean bar; + + @Column(nullable = false) + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Id + public long getId() { + return this.id; + } + public void setId(final long id) { + this.id = id; + } + + public boolean isBar() { + return this.bar; + } + public void setBar(final boolean bar) { + this.bar = bar; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/query/criteria/internal/hhh11877/HHH111877Test.java b/hibernate-core/src/test/java/org/hibernate/query/criteria/internal/hhh11877/HHH111877Test.java new file mode 100644 index 0000000000..e5af10a0b9 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/query/criteria/internal/hhh11877/HHH111877Test.java @@ -0,0 +1,44 @@ +package org.hibernate.query.criteria.internal.hhh11877; + +import javax.persistence.TypedQuery; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Root; + +import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; + +import org.hibernate.testing.TestForIssue; +import org.junit.Test; + +import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; + +/** + * @author Archie Cobbs + * @author Nathan Xu + */ +@TestForIssue( jiraKey = "HHH-11877" ) +public class HHH111877Test extends BaseEntityManagerFunctionalTestCase { + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { Foo.class }; + } + + @Test + public void testNoExceptionThrow() { + doInJPA( this::entityManagerFactory, entityManager -> { + final CriteriaBuilder cb = entityManager.getCriteriaBuilder(); + + final CriteriaQuery cq = cb.createQuery( Foo.class ); + final Root foo = cq.from( Foo.class ); + + cq.select( foo ).where( cb.and( cb.and(), foo.get( Foo_.bar ) ) ); + + final TypedQuery tq = entityManager.createQuery( cq ); + + // without fixing, the statement below will throw exception: + // java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected AST node: . near line 1, column 106 [select generatedAlias0 from org.hibernate.bugs.Foo as generatedAlias0 where ( 1=1 ) and ( generatedAlias0.bar )] + tq.getResultList(); + } ); + } +}