mirror of https://github.com/apache/openjpa.git
OPENJPA-1013: add more test scenarios for parameter settting in Criteria Query
git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@775367 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
f6c64474ed
commit
78f0f54dc3
|
@ -25,7 +25,7 @@ import javax.persistence.GeneratedValue;
|
||||||
import javax.persistence.Id;
|
import javax.persistence.Id;
|
||||||
import javax.persistence.OneToMany;
|
import javax.persistence.OneToMany;
|
||||||
import javax.persistence.OneToOne;
|
import javax.persistence.OneToOne;
|
||||||
import javax.persistence.OrderBy;
|
import javax.persistence.OrderColumn;
|
||||||
import javax.persistence.Table;
|
import javax.persistence.Table;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
|
@ -39,7 +39,7 @@ public class CreditCard {
|
||||||
private Customer customer;
|
private Customer customer;
|
||||||
|
|
||||||
@OneToMany
|
@OneToMany
|
||||||
@OrderBy
|
@OrderColumn
|
||||||
private List<TransactionHistory> transactionHistory;
|
private List<TransactionHistory> transactionHistory;
|
||||||
|
|
||||||
public long getId() {
|
public long getId() {
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package org.apache.openjpa.persistence.criteria;
|
package org.apache.openjpa.persistence.criteria;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import javax.persistence.EntityManager;
|
import javax.persistence.EntityManager;
|
||||||
|
@ -413,7 +414,7 @@ public class TestTypesafeCriteria extends SQLListenerTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
@AllowFailure
|
@AllowFailure
|
||||||
public void testParameters() {
|
public void testParameters1() {
|
||||||
String jpql = "SELECT c FROM Customer c Where c.status = :stat";
|
String jpql = "SELECT c FROM Customer c Where c.status = :stat";
|
||||||
CriteriaQuery q = cb.create();
|
CriteriaQuery q = cb.create();
|
||||||
Root<Customer> c = q.from(Customer.class);
|
Root<Customer> c = q.from(Customer.class);
|
||||||
|
@ -422,6 +423,56 @@ public class TestTypesafeCriteria extends SQLListenerTestCase {
|
||||||
|
|
||||||
assertEquivalence(q, jpql, new String[]{"stat"}, new Object[] {1});
|
assertEquivalence(q, jpql, new String[]{"stat"}, new Object[] {1});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@AllowFailure
|
||||||
|
public void testParameters2() {
|
||||||
|
String jpql = "SELECT c FROM Customer c Where c.status = :stat AND c.name = :name";
|
||||||
|
CriteriaQuery q = cb.create();
|
||||||
|
Root<Customer> c = q.from(Customer.class);
|
||||||
|
Parameter<Integer> param1 = cb.parameter(Integer.class, "stat");
|
||||||
|
Parameter<String> param2 = cb.parameter(String.class, "name");
|
||||||
|
q.select(c).where(cb.equal(c.get(Customer_.status), param1),
|
||||||
|
cb.equal(c.get(Customer_.name), param2));
|
||||||
|
|
||||||
|
assertEquivalence(q, jpql, new String[] { "stat", "name" }, new Object[] { 1, "test" });
|
||||||
|
}
|
||||||
|
|
||||||
|
@AllowFailure
|
||||||
|
public void testParameters3() {
|
||||||
|
String jpql = "SELECT c FROM Customer c Where c.status = :1";
|
||||||
|
CriteriaQuery q = cb.create();
|
||||||
|
Root<Customer> c = q.from(Customer.class);
|
||||||
|
Parameter<Integer> param = cb.parameter(Integer.class);
|
||||||
|
q.select(c).where(cb.equal(c.get(Customer_.status), param));
|
||||||
|
assertEquivalence(q, jpql, new Object[] { 1 });
|
||||||
|
}
|
||||||
|
|
||||||
|
@AllowFailure
|
||||||
|
public void testParameters4() {
|
||||||
|
String jpql = "SELECT c FROM Customer c Where c.status = :1 AND c.name = :2";
|
||||||
|
CriteriaQuery q = cb.create();
|
||||||
|
Root<Customer> c = q.from(Customer.class);
|
||||||
|
Parameter<Integer> param1 = cb.parameter(Integer.class);
|
||||||
|
Parameter<Integer> param2 = cb.parameter(Integer.class);
|
||||||
|
q.select(c).where(cb.equal(c.get(Customer_.status), param1),
|
||||||
|
cb.equal(c.get(Customer_.name), param2));
|
||||||
|
assertEquivalence(q, jpql, new Object[] { 1, "test" });
|
||||||
|
}
|
||||||
|
|
||||||
|
// collection-valued input parameter
|
||||||
|
@AllowFailure
|
||||||
|
public void testParameters5() {
|
||||||
|
String jpql = "SELECT c FROM Customer c Where c.status IN (:coll)";
|
||||||
|
CriteriaQuery q = cb.create();
|
||||||
|
Root<Customer> c = q.from(Customer.class);
|
||||||
|
Parameter<List> param1 = cb.parameter(List.class);
|
||||||
|
q.select(c).where(cb.equal(c.get(Customer_.status), param1));
|
||||||
|
List vals = new ArrayList();
|
||||||
|
vals.add(1);
|
||||||
|
vals.add(2);
|
||||||
|
|
||||||
|
assertEquivalence(q, jpql, new String[] {"coll"}, new Object[] {vals});
|
||||||
|
}
|
||||||
|
|
||||||
@AllowFailure
|
@AllowFailure
|
||||||
public void testSelectList1() {
|
public void testSelectList1() {
|
||||||
|
@ -439,7 +490,8 @@ public class TestTypesafeCriteria extends SQLListenerTestCase {
|
||||||
inv.key().get(Movie_.title), inv.value());
|
inv.key().get(Movie_.title), inv.value());
|
||||||
|
|
||||||
assertEquivalence(q, jpql);
|
assertEquivalence(q, jpql);
|
||||||
}
|
}
|
||||||
|
|
||||||
@AllowFailure
|
@AllowFailure
|
||||||
public void testSelectList2() {
|
public void testSelectList2() {
|
||||||
String jpql = "SELECT NEW CustomerDetails(c.id, c.status, o.quantity) FROM " +
|
String jpql = "SELECT NEW CustomerDetails(c.id, c.status, o.quantity) FROM " +
|
||||||
|
@ -676,6 +728,28 @@ public class TestTypesafeCriteria extends SQLListenerTestCase {
|
||||||
assertEquals(jSQL, cSQL);
|
assertEquals(jSQL, cSQL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void assertEquivalence(CriteriaQuery c, String jpql,
|
||||||
|
Object[] params) {
|
||||||
|
sql.clear();
|
||||||
|
Query q = em.createQuery(c);
|
||||||
|
for (int i = 0; i < params.length; i++) {
|
||||||
|
q.setParameter(i+1, params[i]);
|
||||||
|
}
|
||||||
|
List cList = q.getResultList();
|
||||||
|
assertEquals(1, sql.size());
|
||||||
|
String cSQL = sql.get(0);
|
||||||
|
|
||||||
|
sql.clear();
|
||||||
|
q = em.createQuery(jpql);
|
||||||
|
for (int i = 0; i < params.length; i++) {
|
||||||
|
q.setParameter(i+1, params[i]);
|
||||||
|
}
|
||||||
|
List jList = q.getResultList();
|
||||||
|
assertEquals(1, sql.size());
|
||||||
|
String jSQL = sql.get(0);
|
||||||
|
|
||||||
|
assertEquals(jSQL, cSQL);
|
||||||
|
}
|
||||||
|
|
||||||
void assertEquivalence(CriteriaQuery c, String jpql) {
|
void assertEquivalence(CriteriaQuery c, String jpql) {
|
||||||
sql.clear();
|
sql.clear();
|
||||||
|
|
Loading…
Reference in New Issue