mirror of https://github.com/apache/openjpa.git
OPENJPA-1828: Query with expression IN (collection_valued_input_parameter) gives wrong result when executed the second time
git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@1023914 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
0f5c5291ae
commit
60e88f82fa
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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.
|
||||
|
|
@ -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<Customer> q = cb.createQuery(Customer.class);
|
||||
Root<Customer> c = q.from(Customer.class);
|
||||
|
|
|
@ -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<String> params = new ArrayList<String>();
|
||||
for (int i = 0; i < DEPARTMENT_NAMES.length; i++)
|
||||
params.add(DEPARTMENT_NAMES[i]);
|
||||
|
||||
List<String> rs = null;
|
||||
List<String> rs2 = null;
|
||||
try {
|
||||
rs = (List<String>) 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<String>) 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<String> params2 = new ArrayList<String>();
|
||||
for (int i = 0; i < COMPANY_NAMES.length; i++)
|
||||
params2.add(COMPANY_NAMES[i]);
|
||||
rs = (List<String>) em.createQuery(jpql2).setParameter("names", params2).getResultList();
|
||||
rs2 = (List<String>) em.createQuery(jpql2).setParameter("names", params2).getResultList();
|
||||
assertEquals(rs.size(), rs2.size());
|
||||
}
|
||||
|
||||
public void testCollectionValuedParameterOfEntities() {
|
||||
OpenJPAEntityManager em = emf.createEntityManager();
|
||||
|
|
Loading…
Reference in New Issue