From 2331d205229f72e7526d03896b5fbdca16ffca2b Mon Sep 17 00:00:00 2001 From: Fay Wang Date: Wed, 13 Jan 2010 20:49:01 +0000 Subject: [PATCH] OPENJPA-1458: fix named parameter problem git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@898936 13f79535-47bb-0310-9956-ffa450edef68 --- .../kernel/jpql/JPQLExpressionBuilder.java | 3 +-- .../org/apache/openjpa/lib/util/OrderedMap.java | 2 +- .../jdbc/query/TestQueryParameterBinding.java | 17 ++++++++++++++++- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/jpql/JPQLExpressionBuilder.java b/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/jpql/JPQLExpressionBuilder.java index ba396d660..b35ed4d42 100644 --- a/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/jpql/JPQLExpressionBuilder.java +++ b/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/jpql/JPQLExpressionBuilder.java @@ -1614,8 +1614,7 @@ public class JPQLExpressionBuilder throw parseException(EX_USER, "bad-positional-parameter", new Object[]{ id }, null); } else { - // otherwise the index is just the current size of the parameters - index = parameterTypes.size()-1; + index = parameterTypes.indexOf(id); } Parameter param = isCollectionValued ? factory.newCollectionValuedParameter(paramKey, TYPE_OBJECT) diff --git a/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/OrderedMap.java b/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/OrderedMap.java index 97775988f..9bc695931 100644 --- a/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/OrderedMap.java +++ b/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/OrderedMap.java @@ -52,7 +52,7 @@ public class OrderedMap implements Map, Serializable { public int indexOf(Object key) { int i = 0; for (K k : _del.keySet()) { - if (key == k) + if (key.equals(k)) return i; i++; } diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/TestQueryParameterBinding.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/TestQueryParameterBinding.java index 9b421af96..a2b2ef261 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/TestQueryParameterBinding.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/TestQueryParameterBinding.java @@ -18,6 +18,8 @@ */ package org.apache.openjpa.persistence.jdbc.query; +import java.util.List; + import javax.persistence.EntityManager; import javax.persistence.Query; @@ -248,7 +250,20 @@ public class TestQueryParameterBinding extends SingleEMFTestCase { fail(q); } - + + public void testRepeatedNamedParameters() { + String JPQL_POSITIONAL = + JPQL + "WHERE p.p1 in (select max(p.p1) from Binder p where p.p1=:p1 AND p.p2=:p2 AND p.p3=:p3) " + + "AND p.p1=:p1 AND p.p2=:p2 AND p.p3=:p3"; + Query q = em.createQuery(JPQL_POSITIONAL); + q.setParameter("p1", INT_VALUE); + q.setParameter("p2", STR_VALUE); + q.setParameter("p3", DBL_VALUE); + List list = q.getResultList(); + assertEquals(1, list.size()); + } + + void assertSetParameterFails(Query q, String name, Object v) { try { q.setParameter(name, v);