mirror of
https://github.com/apache/openjpa.git
synced 2025-02-21 01:15:30 +00:00
OPENJPA-2018 fix handling of Arrays in Select IN statements
git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@1536439 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
a28818b7d5
commit
092e70e0ac
@ -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;
|
||||
}
|
||||
|
@ -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 SingleEMTestCase
|
||||
|
||||
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 SingleEMTestCase
|
||||
}
|
||||
}
|
||||
|
||||
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 SingleEMTestCase
|
||||
|
||||
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 SingleEMTestCase
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
|
@ -302,9 +302,6 @@ public class TestTypesafeCriteria extends CriteriaTest {
|
||||
}
|
||||
|
||||
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 extends CriteriaTest {
|
||||
}
|
||||
|
||||
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 extends CriteriaTest {
|
||||
|
||||
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'";
|
||||
|
Loading…
x
Reference in New Issue
Block a user