From 21b41a50fe101d6003fac68b940032dd9bc1f0f9 Mon Sep 17 00:00:00 2001 From: Pinaki Poddar Date: Wed, 15 Apr 2009 08:34:45 +0000 Subject: [PATCH] OPENJPA-1039: treat AND OR operators as non-associative git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@765085 13f79535-47bb-0310-9956-ffa450edef68 --- .../persistence/criteria/TestCriteria.java | 20 +++++++++++++++++++ .../persistence/query/LogicalPredicate.java | 4 ++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestCriteria.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestCriteria.java index 6a8153ba9..517ee61a8 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestCriteria.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestCriteria.java @@ -28,6 +28,7 @@ import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory; import org.apache.openjpa.persistence.query.DomainObject; import org.apache.openjpa.persistence.query.Expression; import org.apache.openjpa.persistence.query.OpenJPAQueryBuilder; +import org.apache.openjpa.persistence.query.Predicate; import org.apache.openjpa.persistence.query.QueryBuilderImpl; import org.apache.openjpa.persistence.query.QueryDefinition; import org.apache.openjpa.persistence.query.SelectItem; @@ -90,6 +91,25 @@ public class TestCriteria extends SingleEMFTestCase { // do nothing as we may not have a database connection } + public void testLogicalPredicateAssociativity() { + DomainObject e = qb.createQueryDefinition(Employee.class); + Predicate p1 = e.get("salary").greaterThan(100); + Predicate p2 = e.get("rating").equal(5); + Predicate p3 = e.get("name").like("John"); + Predicate w1 = p1.and(p2.or(p3)); + Predicate w2 = (p1.and(p2)).or(p3); + QueryDefinition q1 = e.select(e).where(w1); + String jpql1 = qb.toJPQL(q1); + emf.createEntityManager().createDynamicQuery(q1).getResultList(); + + QueryDefinition q2 = e.select(e).where(w2); + String jpql2 = qb.toJPQL(q2); + System.err.println(jpql1); + System.err.println(jpql2); + assertNotEquals(jpql1, jpql2); + emf.createEntityManager().createDynamicQuery(q2).getResultList(); + } + public void testMultipleDomainOfSameClass() { DomainObject o1 = qb.createQueryDefinition(Order.class); DomainObject o2 = o1.addRoot(Order.class); diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/query/LogicalPredicate.java b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/query/LogicalPredicate.java index f19bdc9c5..df782cf23 100644 --- a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/query/LogicalPredicate.java +++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/query/LogicalPredicate.java @@ -52,7 +52,7 @@ public class LogicalPredicate extends AbstractVisitable } public String asExpression(AliasContext ctx) { - return ((Visitable)_p1).asExpression(ctx) + " " + _op + " " - + ((Visitable)_p2).asExpression(ctx); + return OPEN_BRACE + ((Visitable)_p1).asExpression(ctx) + SPACE + _op + + SPACE + ((Visitable)_p2).asExpression(ctx) + CLOSE_BRACE; } }