openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From strub...@apache.org
Subject svn commit: r1536453 - in /openjpa/branches/2.3.x: ./ openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/openjpa2018/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persi...
Date Mon, 28 Oct 2013 17:59:49 GMT
Author: struberg
Date: Mon Oct 28 17:59:49 2013
New Revision: 1536453

URL: http://svn.apache.org/r1536453
Log:
OPENJPA-2018 fix handling of Arrays in Select IN statements

Modified:
    openjpa/branches/2.3.x/   (props changed)
    openjpa/branches/2.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/InExpression.java
    openjpa/branches/2.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/openjpa2018/OpenJPA2018Test.java
    openjpa/branches/2.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestTypesafeCriteria.java

Propchange: openjpa/branches/2.3.x/
------------------------------------------------------------------------------
  Merged /openjpa/trunk:r1535838,1536439

Modified: openjpa/branches/2.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/InExpression.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/InExpression.java?rev=1536453&r1=1536452&r2=1536453&view=diff
==============================================================================
--- openjpa/branches/2.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/InExpression.java
(original)
+++ openjpa/branches/2.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/InExpression.java
Mon Oct 28 17:59:49 2013
@@ -19,6 +19,7 @@
 package org.apache.openjpa.jdbc.kernel.exps;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Iterator;
@@ -213,10 +214,16 @@ class InExpression
     protected Collection getCollection(ExpContext ctx, ExpState state) {
         Object val = _const.getValue(ctx, state);
 
-        // wrap non-Collection parameters in a Collections so the query
-        // lanuage can permit varargs "in" clauses
-        if (!(val instanceof Collection))
+        if (val != null && val.getClass().isArray()) {
+            // arrays need to re-packaged into Collections to
+            // have a single way of handling all this
+            val = Arrays.asList((Object[]) val);
+        }
+        else if (!(val instanceof Collection)) {
+            // wrap non-Collection parameters in a Collections so the query
+            // lanuage can permit varargs "in" clauses
             val = Collections.singleton(val);
+        }
 
         return (Collection) val;
     }

Modified: openjpa/branches/2.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/openjpa2018/OpenJPA2018Test.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/openjpa2018/OpenJPA2018Test.java?rev=1536453&r1=1536452&r2=1536453&view=diff
==============================================================================
--- openjpa/branches/2.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/openjpa2018/OpenJPA2018Test.java
(original)
+++ openjpa/branches/2.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/openjpa2018/OpenJPA2018Test.java
Mon Oct 28 17:59:49 2013
@@ -29,6 +29,10 @@ import java.util.Arrays;
 import java.util.Collection;
 import java.util.List;
 
+/**
+ * This test verifies that select IN statements with
+ * arrays and Collections are fine.
+ */
 public class OpenJPA2018Test extends SingleEMTestCase
 {
 
@@ -52,7 +56,7 @@ public class OpenJPA2018Test extends Sin
 
             TypedQuery<User2018> query = em.createQuery(criteria);
             for (ParameterExpression parameter : criteria.getParameters()) {
-                query.setParameter(parameter, new Long[] { user.id });
+                query.setParameter(parameter, new Long[] { user.id, 123456789L });
             }
 
             List<User2018> result = query.getResultList();
@@ -62,6 +66,24 @@ public class OpenJPA2018Test extends Sin
         }
     }
 
+    public void testInJpqlWithArray() {
+        em.getTransaction().begin();
+        try {
+            User2018 user = new User2018();
+            em.persist(user);
+            em.flush();
+
+            TypedQuery<User2018> query = em.createQuery("select u from User2018 as
u where u.id in (:userIds)",
+                                                        User2018.class);
+            query.setParameter("userIds", new Long[] { user.id, 123456789L });
+
+            List<User2018> result = query.getResultList();
+            assertTrue(!result.isEmpty());
+        } finally {
+            em.getTransaction().commit();
+        }
+    }
+
     public void testInCriteriaWithCollection() {
         em.getTransaction().begin();
         try {
@@ -77,7 +99,7 @@ public class OpenJPA2018Test extends Sin
 
             TypedQuery<User2018> query = em.createQuery(criteria);
             for (ParameterExpression parameter : criteria.getParameters()) {
-                query.setParameter(parameter, Arrays.asList(user.id));
+                query.setParameter(parameter, Arrays.asList(user.id, 123456789L));
             }
 
             List<User2018> result = query.getResultList();
@@ -87,6 +109,25 @@ public class OpenJPA2018Test extends Sin
         }
     }
 
+    public void testInJpqlWithCollection() {
+        em.getTransaction().begin();
+        try {
+            User2018 user = new User2018();
+            em.persist(user);
+            em.flush();
+
+            TypedQuery<User2018> query = em.createQuery("select u from User2018 as
u where u.id in (:userIds)",
+                    User2018.class);
+            query.setParameter("userIds", Arrays.asList(user.id, 123456789L));
+
+            List<User2018> result = query.getResultList();
+            assertTrue(!result.isEmpty());
+        } finally {
+            em.getTransaction().commit();
+        }
+    }
+
+
     public void testId() {
         em.getTransaction().begin();
         try {

Modified: openjpa/branches/2.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestTypesafeCriteria.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestTypesafeCriteria.java?rev=1536453&r1=1536452&r2=1536453&view=diff
==============================================================================
--- openjpa/branches/2.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestTypesafeCriteria.java
(original)
+++ openjpa/branches/2.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestTypesafeCriteria.java
Mon Oct 28 17:59:49 2013
@@ -302,9 +302,6 @@ public class TestTypesafeCriteria extend
     }
 
     public void testJoinedPathInProjection() {
-        String jpql1 = "SELECT p.vendor FROM Employee e JOIN e.contactInfo.phones p "
-                     + "WHERE e.contactInfo.address.zipCode = '95054'";
-
         String jpql = "SELECT p.vendor FROM Employee e JOIN e.contactInfo c JOIN c.phones
p "
                     + "WHERE c.address.zipCode = '95054'";
 
@@ -319,9 +316,6 @@ public class TestTypesafeCriteria extend
     }
 
     public void testKeyExpression() {
-//        String jpql =
-//            "SELECT i.name, p FROM Item i JOIN i.photos p WHERE KEY(p) "
-//            + "LIKE '%egret%'";
         String jpql = "select i.name, VALUE(p) from Item i join i.photos p where KEY(p) like
'egret'";
 
         CriteriaQuery<Tuple> q = cb.createTupleQuery();
@@ -431,7 +425,7 @@ public class TestTypesafeCriteria extend
 
     public void testCaseExpression() {
         String jpql = "SELECT e.name, CASE " 
-        		+ "WHEN e.rating = 1 THEN e.salary * 1.1 "
+            + "WHEN e.rating = 1 THEN e.salary * 1.1 "
                 + "WHEN e.rating = 2 THEN e.salary * 1.2 "
                 + "ELSE e.salary * 1.01 END "
             + "FROM Employee e WHERE e.department.name = 'Engineering'";



Mime
View raw message