diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Param.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Param.java index 3a2f651e3..187c04cb9 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Param.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Param.java @@ -27,6 +27,7 @@ import org.apache.openjpa.jdbc.sql.SQLBuffer; import org.apache.openjpa.jdbc.sql.Select; import org.apache.openjpa.kernel.Filters; import org.apache.openjpa.kernel.exps.Parameter; +import org.apache.openjpa.lib.util.Localizer; import org.apache.openjpa.util.ImplHelper; /** @@ -38,6 +39,7 @@ public class Param extends Const implements Parameter { + private static final Localizer _loc = Localizer.forPackage(Param.class); private final Object _key; private Class _type = null; private int _idx = -1; @@ -136,6 +138,10 @@ public class Param pstate.sqlValue = mapping.toDataStoreValue(val, mapping.getPrimaryKeyColumns(), ctx.store); pstate.otherLength = mapping.getPrimaryKeyColumns().length; + } else if (val instanceof Collection) { + throw new IllegalArgumentException(_loc.get( + "collection-param-not-allowed", _key).toString()); + } else pstate.sqlValue = val; } diff --git a/openjpa-jdbc/src/main/resources/org/apache/openjpa/jdbc/kernel/exps/localizer.properties b/openjpa-jdbc/src/main/resources/org/apache/openjpa/jdbc/kernel/exps/localizer.properties index d2a51b649..3a66ba63a 100644 --- a/openjpa-jdbc/src/main/resources/org/apache/openjpa/jdbc/kernel/exps/localizer.properties +++ b/openjpa-jdbc/src/main/resources/org/apache/openjpa/jdbc/kernel/exps/localizer.properties @@ -50,3 +50,7 @@ type-in-expression-unsupported: The argument "{0}" in TYPE discriminator is in a collection-param-unsupported: The collection parameter used in IN expression is not supported \ for table-per-class inheritance hierarchy or inheritance type joined strategy. \ Use TYPE equal comparison(s) and combine them with OR operators for polymorphic query results. +collection-param-not-allowed: Invalid input parameter "{0}". \ + A collection valued parameter syntax may incorrectly used in the query string. \ + If the parameter is parenthesized, remove the parentheses and try again. + \ No newline at end of file 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 dfc357d6f..dffe05f74 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 @@ -607,7 +607,7 @@ public class TestTypesafeCriteria extends CriteriaTest { } public void testParameters5() { - String jpql = "SELECT c FROM Customer c Where c.status IN (:coll)"; + String jpql = "SELECT c FROM Customer c Where c.status IN :coll"; CriteriaQuery q = cb.createQuery(Customer.class); Root c = q.from(Customer.class); diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/TestPreparedQueryCache.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/TestPreparedQueryCache.java index a1f22e515..eea912963 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/TestPreparedQueryCache.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/TestPreparedQueryCache.java @@ -228,6 +228,38 @@ public class TestPreparedQueryCache extends TestCase { em.close(); super.tearDown(); } + + public void testCollectionValuedParams() { + OpenJPAEntityManager em = emf.createEntityManager(); + String jpql = "select c.name from Department c where c.name in (:names) order by c.name"; + List params = new ArrayList(); + for (int i = 0; i < DEPARTMENT_NAMES.length; i++) + params.add(DEPARTMENT_NAMES[i]); + + List rs = null; + List rs2 = null; + try { + rs = (List) em.createQuery(jpql).setParameter("names", params).getResultList(); + } catch (Exception e) { + // as expected - syntax for collection valued parameter should be :names; + } + assertNull(rs); + + try { + rs2 = (List) em.createQuery(jpql).setParameter("names", params).getResultList(); + } catch (Exception e) { + // as expected - syntax for collection valued parameter should be :names; + } + assertNull(rs2); + + String jpql2 = "select c.name from Company c where c.name in :names order by c.name"; + List params2 = new ArrayList(); + for (int i = 0; i < COMPANY_NAMES.length; i++) + params2.add(COMPANY_NAMES[i]); + rs = (List) em.createQuery(jpql2).setParameter("names", params2).getResultList(); + rs2 = (List) em.createQuery(jpql2).setParameter("names", params2).getResultList(); + assertEquals(rs.size(), rs2.size()); + } public void testCollectionValuedParameterOfEntities() { OpenJPAEntityManager em = emf.createEntityManager();