openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From strub...@apache.org
Subject [openjpa] branch master updated: OPENJPA-2788 fix anynomous Criteria Parameters
Date Mon, 14 Dec 2020 13:16:30 GMT
This is an automated email from the ASF dual-hosted git repository.

struberg pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/openjpa.git


The following commit(s) were added to refs/heads/master by this push:
     new 00bd91c  OPENJPA-2788 fix anynomous Criteria Parameters
00bd91c is described below

commit 00bd91cc2c858949486acb4e8063176be9a48842
Author: Mark Struberg <struberg@apache.org>
AuthorDate: Mon Dec 14 14:13:32 2020 +0100

    OPENJPA-2788 fix anynomous Criteria Parameters
---
 .../persistence/criteria/TestTypesafeCriteria.java | 38 +++++++++++++++++++---
 .../criteria/ParameterExpressionImpl.java          | 24 +++++---------
 2 files changed, 43 insertions(+), 19 deletions(-)

diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestTypesafeCriteria.java
b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestTypesafeCriteria.java
index 5f2f8e5..c0b6cd8 100644
--- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestTypesafeCriteria.java
+++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestTypesafeCriteria.java
@@ -26,6 +26,7 @@ import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Random;
 
 import javax.persistence.Parameter;
 import javax.persistence.Query;
@@ -580,6 +581,36 @@ public class TestTypesafeCriteria extends CriteriaTest {
         }, q, jpql);
     }
 
+    public void testParameters_wo_paramName() {
+        final int rand = new Random().nextInt();
+        final String name = "testName_" + rand;
+        final String lastName = "lastName_" + rand;
+        em.getTransaction().begin();
+        Customer cNew = new Customer();
+        cNew.setName(name);
+        cNew.setLastName(lastName);
+        cNew.setStatus(4711);
+        em.persist(cNew);
+        em.getTransaction().commit();
+
+        CriteriaQuery<Customer> q = cb.createQuery(Customer.class);
+        Root<Customer> c = q.from(Customer.class);
+        Parameter<String> paramName = cb.parameter(String.class);
+        Parameter<String> paramLastName = cb.parameter(String.class);
+        q.select(c).where(cb.and(cb.equal(c.get(Customer_.lastName), paramLastName),
+                cb.equal(c.get(Customer_.name), paramName)));
+
+        final TypedQuery<Customer> query = em.createQuery(q);
+        query.setParameter(paramName, name);
+        query.setParameter(paramLastName, lastName);
+
+        System.err.println("CQ: " + query.toString());
+
+        final List<Customer> customers = query.getResultList();
+        assertNotNull(customers);
+        assertEquals(1, customers.size());
+    }
+
     public void testParameters3() {
         String jpql = "SELECT c FROM Customer c Where c.status = :stat";
 
@@ -1608,10 +1639,9 @@ public class TestTypesafeCriteria extends CriteriaTest {
 
     public void testIdClass() {
         String jpql = "select p from EntityWithIdClass p";
-
-    	CriteriaQuery<EntityWithIdClass> cq = cb.createQuery(EntityWithIdClass.class);
-    	Root<EntityWithIdClass> c = cq.from(EntityWithIdClass.class);
-    	em.createQuery(cq).getResultList();
+        CriteriaQuery<EntityWithIdClass> cq = cb.createQuery(EntityWithIdClass.class);
+        Root<EntityWithIdClass> c = cq.from(EntityWithIdClass.class);
+        em.createQuery(cq).getResultList();
 
         assertEquivalence(cq, jpql);
     }
diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/ParameterExpressionImpl.java
b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/ParameterExpressionImpl.java
index 77b57d1..c748a0b 100644
--- a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/ParameterExpressionImpl.java
+++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/ParameterExpressionImpl.java
@@ -41,7 +41,7 @@ import org.apache.openjpa.util.InternalException;
  */
 class ParameterExpressionImpl<T> extends ExpressionImpl<T>
     implements ParameterExpression<T>, BindableParameter {
-    private String _name;
+    private final String _name;
     private int _index = 0; // index of the parameter as seen by the kernel, not position
     private Object value;
 
@@ -53,8 +53,10 @@ class ParameterExpressionImpl<T> extends ExpressionImpl<T>
      */
     public ParameterExpressionImpl(Class<T> cls, String name) {
         super(cls);
-        if (name != null)
+        if (name != null) {
             assertValidName(name);
+        }
+
         _name = name;
     }
 
@@ -148,8 +150,12 @@ class ParameterExpressionImpl<T> extends ExpressionImpl<T>
 
         ParameterExpressionImpl<?> that = (ParameterExpressionImpl<?>) o;
 
-        if (_name != null ? !_name.equals(that._name) : that._name != null)
+        // we treat parameters the same ONLY if they are
+        // * either the same instance (tested above)
+        // * or have the same parameter name in the same tree
+        if (_name == null || !_name.equals(that._name)) {
             return false;
+        }
 
         // if name is given, then we ignore the index
         if (_name == null && _index != that._index)
@@ -161,16 +167,4 @@ class ParameterExpressionImpl<T> extends ExpressionImpl<T>
         return value != null ? value.equals(that.value) : that.value == null;
     }
 
-    @Override
-    public boolean equals(Object o) {
-        if (this == o) {
-            return true;
-        }
-        return false;
-    }
-
-    @Override
-    public int hashCode() {
-        return super.hashCode();
-    }
 }


Mime
View raw message