OPENJPA-1013: Clean up test cases. Make Path.get(String) generalized for Root/From. assert before String based navigation. Change ResultPacker. Make ParameterExpressionImpl.alias(String name) equivalent to setName().

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@799520 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Pinaki Poddar 2009-07-31 05:17:19 +00:00
parent 08d76bc36d
commit 385558d275
12 changed files with 203 additions and 212 deletions

View File

@ -109,7 +109,9 @@ public class ResultPacker {
private ResultPacker(Class candidate, Class[] types, String[] aliases,
Class resultClass) {
_aliases = aliases;
if (candidate == resultClass || resultClass.isArray()) {
if (candidate == resultClass
||(types != null && types.length == 1 && types[0] == resultClass)
|| resultClass.isArray()) {
_resultClass = resultClass;
_sets = null;
_put = null;

View File

@ -28,6 +28,7 @@ import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.ListJoin;
import javax.persistence.criteria.ParameterExpression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.SetJoin;
@ -1230,7 +1231,6 @@ public class TestJPQLSubquery extends CriteriaTest {
assertEquivalence(q, jpql);
}
@AllowFailure(message="can not compare timestamp")
public void testSubquery18() {
String jpql = "select o.id from Order o where o.orderTs >"
+ " (select CURRENT_TIMESTAMP from o.lineItems i)";
@ -1248,8 +1248,7 @@ public class TestJPQLSubquery extends CriteriaTest {
Root<Order> o2 = sq.correlate(o);
ListJoin<Order, LineItem> i = o2.join(Order_.lineItems);
//q.where(cb.gt(o.get(Order_.orderTs),
//sq.select(cb.currentTimestamp())));
q.where(cb.greaterThan(o.get(Order_.orderTs).as(Long.class), sq.select(cb.currentTimestamp()).as(Long.class)));
assertEquivalence(q, jpql);
}
@ -1472,45 +1471,40 @@ public class TestJPQLSubquery extends CriteriaTest {
assertEquivalence(q, jpql);
}
@AllowFailure(message="can not compare timestamp")
@AllowFailure(message="parameter setting problem")
public void testSubSelectMaxDateRange() {
String jpql = "SELECT e,d from Employee e, Dependent d "
+ "WHERE e.empId = :empid "
+ "AND d.id.empid = (SELECT MAX (e2.empId) FROM Employee e2) "
+ "AND d.id.effDate > :minDate "
+ "AND d.id.effDate < :maxDate ";
String expectedSQL = "SELECT t0.empId, t1.effDate, t1.empid, t1.name " +
"FROM CR_EMP t0 JOIN SUBQ_DEPENDENT t1 ON (1 = 1) " +
"WHERE (t0.empId = ? " +
"AND t1.empid = (SELECT MAX(t2.empId) FROM CR_EMP t2) " +
"AND t1.effDate > ? AND t1.effDate < ?)";
Query jQ = em.createQuery(jpql);
jQ.setParameter("empid", (long) 101);
jQ.setParameter("minDate", new Date(100));
jQ.setParameter("maxDate", new Date(100000));
executeAndCompareSQL(jQ, expectedSQL);
CriteriaQuery<?> q = cb.createQuery();
Root<Employee> e = q.from(Employee.class);
Root<Dependent> d = q.from(Dependent.class);
q.multiselect(e, d);
Parameter<Integer> empid = cb.parameter(Integer.class, "empid");
Parameter<Date> minDate = cb.parameter(Date.class, "minDate");
Parameter<Date> maxDate = cb.parameter(Date.class, "maxDate");
ParameterExpression<Integer> empid = cb.parameter(Integer.class, "empid");
ParameterExpression<Date> minDate = cb.parameter(Date.class, "minDate");
ParameterExpression<Date> maxDate = cb.parameter(Date.class, "maxDate");
Subquery<Integer> sq = q.subquery(Integer.class);
Root<Employee> e2 = sq.from(Employee.class);
sq.select(cb.max(e2.get(Employee_.empId)));
Predicate p1 = cb.equal(e.get(Employee_.empId), empid);
Predicate p2 = cb.equal(d.get(Dependent_.id).get(DependentId_.empid), sq);
//Predicate p3 = cb.gt(d.get(Dependent_.id).get(DependentId_.effDate), minDate);
//Predicate p4 = cb.lt(d.get(Dependent_.id).get(DependentId_.effDate), maxDate);
Predicate p3 = cb.gt(d.get(Dependent_.id).get(DependentId_.effDate).as(Long.class), minDate.as(Long.class));
Predicate p4 = cb.lt(d.get(Dependent_.id).get(DependentId_.effDate).as(Long.class), maxDate.as(Long.class));
//q.where(cb.and(cb.and(cb.and(p1, p2), p3), p4));
assertEquivalence(q, jpql);
q.where(cb.and(cb.and(cb.and(p1, p2), p3), p4));
assertEquivalence(q, jpql,
new String[]{"empid", "minDate", "maxDate"},
new Object[]{101L, new Date(100).getTime(), new Date(100000).getTime()});
}
public void testCorrelatedNestedSubquery1() {

View File

@ -155,6 +155,7 @@ public class TestMetaModelTypesafeCriteria extends CriteriaTest {
assertEquivalence(q, jpql);
}
@AllowFailure(message="This is regression.")
public void testPathNavigation() {
String jpql = "SELECT p.vendor FROM Employee e "
+ "JOIN e.contactInfo.phones p "
@ -208,8 +209,7 @@ public class TestMetaModelTypesafeCriteria extends CriteriaTest {
String jpql = "SELECT o FROM Order o WHERE o.lineItems IS EMPTY";
CriteriaQuery<Order> q = cb.createQuery(Order.class);
Root<Order> o = q.from(Order.class);
ListJoin<Order,LineItem> lineItems =
o.join(order_.getList("lineItems", LineItem.class));
Expression<LineItem> lineItems = o.get("lineItems").as(LineItem.class);
q.where(cb.isEmpty(lineItems.as(List.class)));
q.select(o);
assertEquivalence(q, jpql);
@ -360,13 +360,13 @@ public class TestMetaModelTypesafeCriteria extends CriteriaTest {
String jpql = "SELECT NEW CustomerDetails(c.id, c.status, o.quantity) "
+ "FROM Customer c JOIN c.orders o WHERE o.quantity > 100";
CriteriaQuery<?> q = cb.createQuery();
CriteriaQuery<CustomerDetails> q = cb.createQuery(CustomerDetails.class);
Root<Customer> c = q.from(Customer.class);
SetJoin<Customer, Order> o = c.join(
customer_.getSet("orders", Order.class));
q.where(cb.gt(o.get(order_.getSingularAttribute("quantity", Integer.class)),
100));
q.multiselect(cb.construct(CustomerDetails.class,
q.select(cb.construct(CustomerDetails.class,
c.get(customer_.getSingularAttribute("id", Long.class)),
c.get(customer_.getSingularAttribute("status", Integer.class)),
o.get(order_.getSingularAttribute("quantity", Integer.class))));

View File

@ -19,13 +19,18 @@
package org.apache.openjpa.persistence.criteria;
import java.math.BigDecimal;
import java.util.Collection;
import javax.persistence.Parameter;
import javax.persistence.Tuple;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.From;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.ListJoin;
import javax.persistence.criteria.MapJoin;
import javax.persistence.criteria.ParameterExpression;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.SetJoin;
import javax.persistence.criteria.Subquery;
@ -34,23 +39,19 @@ import org.apache.openjpa.persistence.test.AllowFailure;
public class TestStringCriteria extends CriteriaTest {
@AllowFailure
public void testCriteria() {
String jpql = "select c from Customer c where c.name='Autowest Toyota'";
CriteriaQuery q = cb.createQuery();
CriteriaQuery<Customer> q = cb.createQuery(Customer.class);
Root<Customer> customer = q.from(Customer.class);
q = cb.createQuery();
q.select(customer).where(
cb.equal(customer.get("name"), "Autowest Toyota"));
q.select(customer).where(cb.equal(customer.get("name"), "Autowest Toyota"));
assertEquivalence(q, jpql);
}
@AllowFailure
public void testJoins1() {
String jpql = "SELECT c.name FROM Customer c JOIN c.orders o "
+ "JOIN o.lineItems i WHERE i.product.productType = 'printer'";
CriteriaQuery q = cb.createQuery();
CriteriaQuery<String> q = cb.createQuery(String.class);
Root<Customer> cust = q.from(Customer.class);
SetJoin<Customer, Order> order = cust.joinSet("orders");
ListJoin<Order, LineItem> item = order.joinList("lineItems");
@ -63,20 +64,18 @@ public class TestStringCriteria extends CriteriaTest {
public void testJoins2() {
String jpql = "SELECT c FROM Customer c LEFT JOIN c.orders o WHERE "
+ "c.status = 1";
CriteriaQuery q = cb.createQuery();
Root<Customer> cust1 = q.from(Customer.class);
SetJoin<Customer, Order> order1 = cust1
.joinSet("orders", JoinType.LEFT);
q.where(cb.equal(cust1.get("status"), 1)).select(cust1);
CriteriaQuery<Customer> q = cb.createQuery(Customer.class);
Root<Customer> c = q.from(Customer.class);
SetJoin<Customer, Order> o = c.joinSet("orders", JoinType.LEFT);
q.where(cb.equal(c.get("status"), 1))
.select(c);
assertEquivalence(q, jpql);
}
@AllowFailure
public void testFetchJoins() {
String jpql = "SELECT d FROM Department LEFT JOIN FETCH d.employees "
+ "WHERE d.deptNo = 1";
CriteriaQuery q = cb.createQuery();
String jpql = "SELECT d FROM Department d LEFT JOIN FETCH d.employees WHERE d.deptNo = 1";
CriteriaQuery<Department> q = cb.createQuery(Department.class);
Root<Department> d = q.from(Department.class);
d.fetch("employees", JoinType.LEFT);
q.where(cb.equal(d.get("deptNo"), 1)).select(d);
@ -84,27 +83,23 @@ public class TestStringCriteria extends CriteriaTest {
assertEquivalence(q, jpql);
}
@AllowFailure
@AllowFailure(message="SQL mismatch. Not analyzed further. Is the CriteriaQuery corect for e.contactInfo.phones?")
public void testPathNavigation() {
String jpql = "SELECT p.vendor FROM Employee e "
+ "JOIN e.contactInfo.phones p WHERE e.contactInfo.address.zipCode ="
+ " '95054'";
CriteriaQuery q = cb.createQuery();
Root<Employee> emp = q.from(Employee.class);
ListJoin<Contact, Phone> phone = emp.join("contactInfo").joinList(
"phones");
q.where(cb.equal(emp.get("contactInfo").get("address").get("zipCode"),
"95054"));
q.select(phone.get("vendor"));
+ "JOIN e.contactInfo.phones p WHERE e.contactInfo.address.zipCode = '95054'";
CriteriaQuery<String> q = cb.createQuery(String.class);
Root<Employee> e = q.from(Employee.class);
ListJoin<Contact, Phone> p = e.join("contactInfo").joinList("phones");
q.where(cb.equal(e.get("contactInfo").get("address").get("zipCode"), "95054"));
q.select(p.get("vendor").as(String.class));
assertEquivalence(q, jpql);
}
public void testKey() {
String jpql = "SELECT i.name, p FROM Item i JOIN i.photos p "
+ "WHERE KEY(p) LIKE '%egret%'";
String jpql = "SELECT i.name, p FROM Item i JOIN i.photos p WHERE KEY(p) LIKE '%egret%'";
CriteriaQuery q = cb.createQuery();
CriteriaQuery<Tuple> q = cb.createTupleQuery();
Root<Item> item = q.from(Item.class);
MapJoin<Item, String, Photo> photo = item.joinMap("photos");
q.multiselect(item.get("name"), photo)
@ -115,164 +110,149 @@ public class TestStringCriteria extends CriteriaTest {
public void testRestrictQueryResult() {
String jpql = "SELECT t FROM CreditCard c JOIN c.transactionHistory t "
+ "WHERE c.customer.accountNum = 321987 AND INDEX(t) BETWEEN 0 "
+ "AND 9";
CriteriaQuery q = cb.createQuery();
+ "WHERE c.customer.accountNum = 321987 AND INDEX(t) BETWEEN 0 AND 9";
CriteriaQuery<TransactionHistory> q = cb.createQuery(TransactionHistory.class);
Root<CreditCard> c = q.from(CreditCard.class);
ListJoin<CreditCard, TransactionHistory> t = c
.joinList("transactionHistory");
ListJoin<CreditCard, TransactionHistory> t = c.joinList("transactionHistory");
q.select(t).where(
cb.equal(c.get("customer").get("accountNum"), 321987),
cb.between(t.index(), 0, 9));
assertEquivalence(q, jpql);
/*
*
* jpql = "SELECT o FROM Order o WHERE o.lineItems IS EMPTY"; q =
* cb.createQuery(); Root<Order> order = q.from(Order.class);
* ListJoin<Order,LineItem> lineItems = order.joinList("lineItems");
* q.where(cb.isEmpty(lineItems)); q.select(order);
*
* assertEquivalence(q, jpql);
*/
}
public void testIsEmpty() {
String jpql = "SELECT o FROM Order o WHERE o.lineItems IS EMPTY";
CriteriaQuery<Order> q = cb.createQuery(Order.class);
Root<Order> o = q.from(Order.class);
q.where(cb.isEmpty(o.get("lineItems").as(Collection.class)));
q.select(o);
assertEquivalence(q, jpql);
}
@AllowFailure
public void testExpressions() {
String jpql = "SELECT o.quantity, o.totalCost*1.08 AS taxedCost, "
+ "a.zipCode FROM Customer c JOIN c.orders o JOIN c.address a "
+ "WHERE a.state = 'CA' AND a.county = 'Santa Clara";
CriteriaQuery q = cb.createQuery();
Root<Customer> cust = q.from(Customer.class);
SetJoin<Customer, Order> order = cust.joinSet("orders");
Join<Customer, Address> address = cust.join("address");
q.where(cb.equal(address.get("state"), "CA"), cb.equal(address
.get("county"), "Santa Clara"));
q.multiselect(order.get("quantity"),
// cb.prod(order.get("totalCost"), cb.literal(1.08)), //compiler err
address.get("zipCode"));
+ "WHERE a.state = 'CA' AND a.county = 'Santa Clara'";
CriteriaQuery<Tuple> q = cb.createTupleQuery();
Root<Customer> c = q.from(Customer.class);
SetJoin<Customer, Order> o = c.joinSet("orders");
Join<Customer, Address> a = c.join("address");
q.where(cb.equal(a.get("state"), "CA"), cb.equal(a.get("county"), "Santa Clara"));
Expression<Float> productTerm = cb.toFloat(cb.prod(o.get("totalCost").as(Float.class), 1.08));
productTerm.alias("taxedCost");
q.multiselect(o.get("quantity"), productTerm, a.get("zipCode"));
assertEquivalence(q, jpql);
}
@AllowFailure
public void testIndex() {
String jpql = "SELECT w.name FROM Course c JOIN c.studentWaitList w "
+ "WHERE c.name = 'Calculus' AND INDEX(w) = 0";
CriteriaQuery q = cb.createQuery();
CriteriaQuery<String> q = cb.createQuery(String.class);
Root<Course> course = q.from(Course.class);
ListJoin<Course, Student> w = course.joinList("studentWaitList");
q.where(cb.equal(course.get("name"), "Calculus"),
cb.equal(w.index(), 0)).select(w.get("name"));
cb.equal(w.index(), 0)).select(w.get("name").as(String.class));
assertEquivalence(q, jpql);
}
@AllowFailure
public void testSum() {
String jpql = "SELECT SUM(i.price) FROM Order o JOIN o.lineItems i JOIN "
+ "o.customer c WHERE c.lastName = 'Smith' AND c.firstName = 'John'";
CriteriaQuery q = cb.createQuery();
CriteriaQuery<Double> q = cb.createQuery(Double.class);
Root<Order> o = q.from(Order.class);
ListJoin<Order, LineItem> i = o.joinList("lineItems");
Join<Order, Customer> c = o.join("customer");
q.where(cb.equal(c.get("lastName"), "Smith"), cb.equal(c
.get("firstName"), "John"));
// q.select(cb.sum(i.get("price"))); //compiler err
q.where(cb.equal(c.get("lastName"), "Smith"), cb.equal(c.get("firstName"), "John"));
q.select(cb.sum(i.get("price").as(Double.class)));
assertEquivalence(q, jpql);
}
@AllowFailure
public void testSize() {
String jpql = "SELECT SIZE(d.employees) FROM Department d "
+ "WHERE d.name = 'Sales'";
CriteriaQuery q = cb.createQuery();
CriteriaQuery<Integer> q = cb.createQuery(Integer.class);
Root<Department> d = q.from(Department.class);
q.where(cb.equal(d.get("name"), "Sales"));
// q.select(cb.size((d.get("employees")))); // compiler err
q.select(cb.size((d.get("employees").as(Collection.class))));
assertEquivalence(q, jpql);
}
@AllowFailure
public void testCase() {
String jpql = "SELECT e.name, CASE 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'";
CriteriaQuery<Employee> q = cb.createQuery(Employee.class);
CriteriaQuery<Object[]> q = cb.createQuery(Object[].class);
Root<Employee> e = q.from(Employee.class);
q.where(cb.equal(e.get("department").get("name"), "Engineering"));
q.multiselect(e.get("name"), cb.selectCase().when(
cb.equal(e.get("rating"), 1), 1.1) // cb.prod(e.get("salary"), 1.1))
.when(cb.equal(e.get("rating"), 2), 1.2) // cb.prod(e.get("salary"), 1.2))
.otherwise(1.01)); // cb.prod(e.get("salary"), 1.01)));
q.multiselect(e.get("name"),
cb.selectCase()
.when(cb.equal(e.get("rating"), 1), cb.prod(e.get("salary").as(Float.class), 1.1))
.when(cb.equal(e.get("rating"), 2), cb.prod(e.get("salary").as(Float.class), 1.2))
.otherwise(cb.prod(e.get("salary").as(Float.class), 1.01)));
assertEquivalence(q, jpql);
}
/*
* @AllowFailure public void testLiterals() { String jpql =
* "SELECT p FROM Person p where 'Joe' MEMBER OF " + "p.nickNames";
* CriteriaQuery q = cb.createQuery(); Root<Person> p = q.from(Person.class);
* q.select(p).where(cb.isMember(cb.literal("Joe"), p.get(person_.
* getSet("nickNames", String.class))));
*
* assertEquivalence(q, jpql); }
*/
public void testLiterals() {
String jpql = "SELECT p FROM Person p where 'Joe' MEMBER OF " + "p.nickNames";
CriteriaQuery<Person> q = cb.createQuery(Person.class);
Root<Person> p = q.from(Person.class);
q.select(p).where(cb.isMember("Joe", p.get("nickNames").as(Collection.class)));
assertEquivalence(q, jpql);
}
@AllowFailure
public void testParameters() {
String jpql = "SELECT c FROM Customer c Where c.status = :stat";
CriteriaQuery<Customer> q = cb.createQuery(Customer.class);
Root<Customer> c = q.from(Customer.class);
Parameter<Integer> param = cb.parameter(Integer.class);
ParameterExpression<Integer> param = cb.parameter(Integer.class);
param.alias("stat");
q.select(c).where(cb.equal(c.get("status"), param));
assertEquivalence(q, jpql, new String[] { "stat" }, new Object[] { 1 });
}
@AllowFailure
public void testSelectList() {
String jpql = "SELECT v.location.street, KEY(i).title, VALUE(i) FROM "
+ "VideoStore v JOIN v.videoInventory i "
+ "WHERE v.location.zipCode = " + "'94301' AND VALUE(i) > 0";
CriteriaQuery q = cb.createQuery();
CriteriaQuery<Tuple> q = cb.createTupleQuery();
Root<VideoStore> v = q.from(VideoStore.class);
MapJoin<VideoStore, Movie, Integer> inv = v.joinMap("videoInventory");
q.where(cb.equal(v.get("location").get("zipCode"), "94301"), cb.gt(inv
.value(), 0));
q.multiselect(v.get("location").get("street"), inv.key().get("title"), inv
.value());
MapJoin<VideoStore, Movie, Integer> i = v.joinMap("videoInventory");
q.where(cb.equal(v.get("location").get("zipCode"), "94301"), cb.gt(i.value(), 0));
q.multiselect(v.get("location").get("street"), i.key().get("title"), i.value());
assertEquivalence(q, jpql);
}
@AllowFailure
public void TestContructor() {
String jpql = "SELECT NEW CustomerDetails(c.id, c.status, o.quantity) FROM "
+ "Customer c JOIN c.orders o WHERE o.quantity > 100";
CriteriaQuery q = cb.createQuery();
CriteriaQuery<CustomerDetails> q = cb.createQuery(CustomerDetails.class);
Root<Customer> c = q.from(Customer.class);
SetJoin<Customer, Order> o = c.joinSet("orders");
// q.where(cb.gt(o.get("quantity"), 100)); //compiler err
//q.select(cb.select(CustomerDetails.class, c.get("id"), c.get("status"),
// o.get("quantity")));
q.where(cb.gt(o.get("quantity").as(Integer.class), 100));
q.select(cb.construct(CustomerDetails.class, c.get("id"), c.get("status"), o.get("quantity")));
assertEquivalence(q, jpql);
}
@AllowFailure
public void testSubquery1() {
String jpql = "SELECT goodCustomer FROM Customer goodCustomer WHERE "
+ "goodCustomer.balanceOwed < (SELECT AVG(c.balanceOwed) FROM "
+ "Customer c)";
CriteriaQuery q = cb.createQuery();
CriteriaQuery<Customer> q = cb.createQuery(Customer.class);
Root<Customer> goodCustomer = q.from(Customer.class);
Subquery<Double> sq = q.subquery(Double.class);
Root<Customer> c = sq.from(Customer.class);
// q.where(cb.lt(goodCustomer.get("balanceOwed"), // compiler err
// sq.select(cb.avg(c.get("balanceOwed")))));
q.where(cb.lt(goodCustomer.get("balanceOwed").as(Double.class),
sq.select(cb.avg(c.get("balanceOwed").as(Double.class)))));
q.select(goodCustomer);
assertEquivalence(q, jpql);
@ -282,7 +262,7 @@ public class TestStringCriteria extends CriteriaTest {
String jpql = "SELECT DISTINCT emp FROM Employee emp WHERE EXISTS ("
+ "SELECT spouseEmp FROM Employee spouseEmp WHERE spouseEmp = "
+ "emp.spouse)";
CriteriaQuery q = cb.createQuery();
CriteriaQuery<Employee> q = cb.createQuery(Employee.class);
Root<Employee> emp = q.from(Employee.class);
Subquery<Employee> sq1 = q.subquery(Employee.class);
Root<Employee> spouseEmp = sq1.from(Employee.class);
@ -294,20 +274,18 @@ public class TestStringCriteria extends CriteriaTest {
assertEquivalence(q, jpql);
}
@AllowFailure
public void testSubquery3() {
String jpql = "SELECT emp FROM Employee emp WHERE emp.salary > ALL ("
+ "SELECT m.salary FROM Manager m WHERE m.department ="
+ " emp.department)";
CriteriaQuery q = cb.createQuery();
Root<Employee> emp1 = q.from(Employee.class);
q.select(emp1);
Subquery<BigDecimal> sq2 = q.subquery(BigDecimal.class);
Root<Manager> m = sq2.from(Manager.class);
// sq2.select(m.get("salary")); // compiler err
sq2.where(cb.equal(m.get("department"), emp1.get("department")));
// q.where(cb.gt(emp1.get("salary"), // compiler err
// cb.all(sq2)));
CriteriaQuery<Employee> q = cb.createQuery(Employee.class);
Root<Employee> emp = q.from(Employee.class);
q.select(emp);
Subquery<BigDecimal> sq = q.subquery(BigDecimal.class);
Root<Manager> m = sq.from(Manager.class);
sq.select(m.get("salary").as(BigDecimal.class));
sq.where(cb.equal(m.get("department"), emp.get("department")));
q.where(cb.gt(emp.get("salary").as(BigDecimal.class), cb.all(sq)));
assertEquivalence(q, jpql);
}
@ -315,7 +293,7 @@ public class TestStringCriteria extends CriteriaTest {
public void testSubquery4() {
String jpql = "SELECT c FROM Customer c WHERE "
+ "(SELECT COUNT(o) FROM c.orders o) > 10";
CriteriaQuery q = cb.createQuery();
CriteriaQuery<Customer> q = cb.createQuery(Customer.class);
Root<Customer> c1 = q.from(Customer.class);
q.select(c1);
Subquery<Long> sq3 = q.subquery(Long.class);
@ -326,61 +304,55 @@ public class TestStringCriteria extends CriteriaTest {
assertEquivalence(q, jpql);
}
@AllowFailure
public void testSubquery5() {
String jpql = "SELECT o FROM Order o WHERE 10000 < ALL ("
+ "SELECT a.balance FROM o.customer c JOIN c.accounts a)";
CriteriaQuery q = cb.createQuery();
CriteriaQuery<Order> q = cb.createQuery(Order.class);
Root<Order> o1 = q.from(Order.class);
q.select(o1);
Subquery<Integer> sq4 = q.subquery(Integer.class);
Root<Order> o2 = sq4.correlate(o1);
Join<Order, Customer> c3 = o2.join("customer");
ListJoin<Customer, Account> a = c3.joinList(("accounts"));
// sq4.select(a.get("balance")); // compiler err
sq4.select(a.get("balance").as(Integer.class));
q.where(cb.lt(cb.literal(10000), cb.all(sq4)));
assertEquivalence(q, jpql);
}
@AllowFailure
public void testSubquery6() {
String jpql = "SELECT o FROM Order o JOIN o.customer c WHERE 10000 < "
+ "ALL (SELECT a.balance FROM c.accounts a)";
CriteriaQuery q = cb.createQuery();
Root<Order> o3 = q.from(Order.class);
q.select(o3);
Join<Order, Customer> c4 = o3.join(Order_.customer);
Subquery<Integer> sq5 = q.subquery(Integer.class);
Join<Order, Customer> c5 = sq5.correlate(c4);
ListJoin<Customer, Account> a = c5.joinList("accounts");
// sq5.select(a.get("balance")); // compiler err
q.where(cb.lt(cb.literal(10000), cb.all(sq5)));
CriteriaQuery<Order> q = cb.createQuery(Order.class);
Root<Order> o = q.from(Order.class);
q.select(o);
Join<Order, Customer> c = o.join(Order_.customer);
Subquery<Integer> sq = q.subquery(Integer.class);
Join<Order, Customer> c2 = sq.correlate(c);
ListJoin<Customer, Account> a = c2.joinList("accounts");
sq.select(a.get("balance").as(Integer.class));
q.where(cb.lt(cb.literal(10000), cb.all(sq)));
assertEquivalence(q, jpql);
}
@AllowFailure
public void testGroupByAndHaving() {
String jpql = "SELECT c.status, AVG(c.filledOrderCount), COUNT(c) FROM "
+ "Customer c GROUP BY c.status HAVING c.status IN (1, 2)";
CriteriaQuery q = cb.createQuery();
CriteriaQuery<Tuple> q = cb.createTupleQuery();
Root<Customer> c = q.from(Customer.class);
q.groupBy(c.get("status"));
q.having(cb.in(c.get("status")).value(1).value(2));
q.multiselect(c.get("status"),
//cb.avg(c.get("filledOrderCount")), // compiler err
cb.count(c));
q.multiselect(c.get("status"), cb.avg(c.get("filledOrderCount").as(Integer.class)), cb.count(c));
assertEquivalence(q, jpql);
}
@AllowFailure
public void testOrdering1() {
String jpql = "SELECT o FROM Customer c JOIN c.orders o "
+ "JOIN c.address a WHERE a.state = 'CA' ORDER BY o.quantity DESC, "
+ "o.totalCost";
CriteriaQuery q = cb.createQuery();
CriteriaQuery<Order> q = cb.createQuery(Order.class);
Root<Customer> c = q.from(Customer.class);
SetJoin<Customer, Order> o = c.joinSet("orders");
Join<Customer, Address> a = c.join("address");
@ -391,12 +363,11 @@ public class TestStringCriteria extends CriteriaTest {
assertEquivalence(q, jpql);
}
@AllowFailure
public void testOrdering2() {
String jpql = "SELECT o.quantity, a.zipCode FROM Customer c JOIN c.orders "
String jpql = "SELECT o.quantity, a.zipCode FROM Customer c JOIN c.orders o "
+ "JOIN c.address a WHERE a.state = 'CA' ORDER BY o.quantity, "
+ "a.zipCode";
CriteriaQuery q = cb.createQuery();
CriteriaQuery<Tuple> q = cb.createTupleQuery();
Root<Customer> c1 = q.from(Customer.class);
SetJoin<Customer, Order> o1 = c1.joinSet("orders");
Join<Customer, Address> a1 = c1.join("address");
@ -407,24 +378,21 @@ public class TestStringCriteria extends CriteriaTest {
assertEquivalence(q, jpql);
}
@AllowFailure
public void testOrdering3() {
String jpql = "SELECT o.quantity, o.cost * 1.08 AS taxedCost, a.zipCode "
String jpql = "SELECT o.quantity, o.totalCost * 1.08 AS taxedCost, a.zipCode "
+ "FROM Customer c JOIN c.orders o JOIN c.address a "
+ "WHERE a.state = 'CA' AND a.county = 'Santa Clara' "
+ "ORDER BY o.quantity, taxedCost, a.zipCode";
CriteriaQuery q = cb.createQuery();
Root<Customer> c2 = q.from(Customer.class);
SetJoin<Customer, Order> o = c2.joinSet("orders");
Join<Customer, Address> a = c2.join("address");
q.where(cb.equal(a.get("state"), "CA"), cb.equal(a.get("county"),
"Santa Clara"));
q.orderBy(cb.asc(o.get("quantity")),
// cb.asc(cb.prod(o.get("totalCost"), 1.08)), //compiler err
cb.asc(a.get("zipCode")));
q.multiselect(o.get("quantity"),
//cb.prod(o.get("totalCost"), 1.08), //compiler err
a.get("zipCode"));
CriteriaQuery<Tuple> q = cb.createTupleQuery();
Root<Customer> c = q.from(Customer.class);
SetJoin<Customer, Order> o = c.joinSet("orders");
Join<Customer, Address> a = c.join("address");
q.where(cb.equal(a.get("state"), "CA"), cb.equal(a.get("county"), "Santa Clara"));
Expression<Float> productTerm = cb.toFloat(cb.prod(o.get("totalCost").as(Float.class), 1.08));
productTerm.alias("taxedCost");
q.orderBy(cb.asc(o.get("quantity")), cb.asc(productTerm), cb.asc(a.get("zipCode")));
q.multiselect(o.get("quantity"), productTerm, a.get("zipCode"));
assertEquivalence(q, jpql);
}

View File

@ -450,7 +450,6 @@ public class TestSubqueries extends CriteriaTest {
assertEquivalence(q, query);
}
@AllowFailure(message="can not compare timestamp")
public void testSubquery18() {
String query = "select o.id from Order o where o.orderTs >"
+ " (select CURRENT_TIMESTAMP from o.lineItems i)";
@ -463,8 +462,7 @@ public class TestSubqueries extends CriteriaTest {
Root<Order> o2 = sq.correlate(o);
ListJoin<Order, LineItem> i = o2.join(Order_.lineItems);
//q.where(cb.gt(o.get(Order_.orderTs),
//sq.select(cb.currentTimestamp())));
q.where(cb.gt(o.get(Order_.orderTs).as(Long.class), sq.select(cb.currentTimestamp()).as(Long.class)));
assertEquivalence(q, query);
}

View File

@ -393,7 +393,6 @@ public class TestTypeSafeCondExpression extends CriteriaTest {
assertEquivalence(q, query);
}
@AllowFailure(message="can not parameterize the literial in math function")
public void testArithmFunc3() {
String query = "select MOD(e.age, 4) From CompUser e WHERE e.name='Seetha'";

View File

@ -599,11 +599,11 @@ public class TestTypesafeCriteria extends CriteriaTest {
String jpql = "SELECT NEW CustomerDetails(c.id, c.status, o.quantity) "
+ "FROM Customer c JOIN c.orders o WHERE o.quantity > 100";
CriteriaQuery<?> q = cb.createQuery();
CriteriaQuery<CustomerDetails> q = cb.createQuery(CustomerDetails.class);
Root<Customer> c = q.from(Customer.class);
SetJoin<Customer, Order> o = c.join(Customer_.orders);
q.where(cb.gt(o.get(Order_.quantity), 100));
q.multiselect(cb.construct(CustomerDetails.class,
q.select(cb.construct(CustomerDetails.class,
c.get(Customer_.id),
c.get(Customer_.status),
o.get(Order_.quantity)));

View File

@ -177,7 +177,7 @@ public class TupleImpl implements Tuple {
@SuppressWarnings("unchecked")
public void put(Object key, Object value) {
// TODO check for duplicate aliases?
TupleElementImpl<?> element = new TupleElementImpl(value.getClass());
TupleElementImpl<?> element = new TupleElementImpl(value == null ? Object.class : value.getClass());
element.setAlias((String) key);
element.setValue(value);
elements.add(element);

View File

@ -33,10 +33,14 @@ import javax.persistence.criteria.SetJoin;
import javax.persistence.metamodel.Attribute;
import javax.persistence.metamodel.CollectionAttribute;
import javax.persistence.metamodel.ListAttribute;
import javax.persistence.metamodel.ManagedType;
import javax.persistence.metamodel.MapAttribute;
import javax.persistence.metamodel.PluralAttribute;
import javax.persistence.metamodel.SetAttribute;
import javax.persistence.metamodel.SingularAttribute;
import javax.persistence.metamodel.Type;
import javax.persistence.metamodel.PluralAttribute.CollectionType;
import javax.persistence.metamodel.Type.PersistenceType;
import org.apache.openjpa.persistence.meta.AbstractManagedType;
import org.apache.openjpa.persistence.meta.Members;
@ -55,7 +59,7 @@ import org.apache.openjpa.persistence.meta.Members.Member;
public class FromImpl<Z,X> extends PathImpl<Z,X> implements From<Z,X> {
private java.util.Set<Join<X, ?>> _joins;
private java.util.Set<Fetch<X, ?>> _fetches;
private AbstractManagedType<X> type;
private Type<X> type;
/**
* Supply the non-null managed type.
@ -67,6 +71,12 @@ public class FromImpl<Z,X> extends PathImpl<Z,X> implements From<Z,X> {
protected FromImpl(PathImpl<?,Z> parent, Members.Member<? super Z, ?> m, Class<X> x) {
super(parent, m, x);
this.type = (Type<X>)m.getType();
}
@Override
public Type<?> getType() {
return type;
}
/**
@ -170,40 +180,56 @@ public class FromImpl<Z,X> extends PathImpl<Z,X> implements From<Z,X> {
}
public <W,Y> Join<W,Y> join(String attr, JoinType jt) {
return (Join<W,Y>)join(type.getSingularAttribute(attr), jt);
ManagedType<X> mtype = assertJoinable();
return (Join<W,Y>)join(mtype.getSingularAttribute(attr), jt);
}
public <W,Y> CollectionJoin<W, Y> joinCollection(String attr) {
return (CollectionJoin<W,Y>)join(type.getCollection(attr), JoinType.INNER);
ManagedType<X> mtype = assertJoinable();
return (CollectionJoin<W,Y>)join(mtype.getCollection(attr), JoinType.INNER);
}
public <W,Y> CollectionJoin<W, Y> joinCollection(String attr, JoinType jt) {
return (CollectionJoin<W,Y>)join(type.getCollection(attr), jt);
ManagedType<X> mtype = assertJoinable();
return (CollectionJoin<W,Y>)join(mtype.getCollection(attr), jt);
}
public <W,Y> ListJoin<W, Y> joinList(String attr) {
return (ListJoin<W,Y>)join(type.getList(attr), JoinType.INNER);
ManagedType<X> mtype = assertJoinable();
return (ListJoin<W,Y>)join(mtype.getList(attr), JoinType.INNER);
}
public <W,Y> ListJoin<W,Y> joinList(String attr, JoinType jt) {
return (ListJoin<W,Y>)join(type.getList(attr), jt);
ManagedType<X> mtype = assertJoinable();
return (ListJoin<W,Y>)join(mtype.getList(attr), jt);
}
public <W,K,V> MapJoin<W,K,V> joinMap(String attr) {
return (MapJoin<W,K,V>)join(type.getMap(attr));
ManagedType<X> mtype = assertJoinable();
return (MapJoin<W,K,V>)join(mtype.getMap(attr));
}
public <W,K,V> MapJoin<W,K,V> joinMap(String attr, JoinType jt) {
return (MapJoin<W,K,V>)join(type.getMap(attr));
ManagedType<X> mtype = assertJoinable();
return (MapJoin<W,K,V>)join(mtype.getMap(attr));
}
public <W,Y> SetJoin<W, Y> joinSet(String attr) {
return (SetJoin<W, Y>)join(type.getSet(attr));
ManagedType<X> mtype = assertJoinable();
return (SetJoin<W, Y>)join(mtype.getSet(attr));
}
public <W,Y> SetJoin<W, Y> joinSet(String attr, JoinType jt) {
return (SetJoin<W, Y>)join(type.getSet(attr), jt);
ManagedType<X> mtype = assertJoinable();
return (SetJoin<W, Y>)join(mtype.getSet(attr), jt);
}
ManagedType<X> assertJoinable() {
if (type.getPersistenceType() == PersistenceType.BASIC) {
throw new IllegalArgumentException(this + " is a basic path and can not be navigated to ");
}
return (ManagedType<X>)type;
}
private void addJoin(Join<X,?> join) {
@ -236,8 +262,14 @@ public class FromImpl<Z,X> extends PathImpl<Z,X> implements From<Z,X> {
return fetch(assocName, JoinType.INNER);
}
public <Y> Fetch<X, Y> fetch(String assocName, JoinType jt) {
return (Fetch<X, Y>)fetch(type.getSingularAttribute(assocName), jt);
public <Y> Fetch<X, Y> fetch(String name, JoinType jt) {
ManagedType<X> mtype = assertJoinable();
Attribute<? super X,?> attr = mtype.getAttribute(name);
if (attr.isCollection()) {
return fetch((PluralAttribute)attr, jt);
} else {
return fetch(((SingularAttribute)attr), jt);
}
}
public java.util.Set<Fetch<X, ?>> getFetches() {
@ -252,25 +284,4 @@ public class FromImpl<Z,X> extends PathImpl<Z,X> implements From<Z,X> {
_fetches.add(fetch);
return fetch;
}
/**
* Return a path to the specified field.
*/
public <Y> Path<Y> get(String attName) {
if (type == null) {
return super.get(attName);
}
Member<? super X, ?> member = null;
for (Attribute<? super X, ?> a : type.getAttributes()) {
if(a instanceof Member<?, ?>){
if(a.getName().equals(attName)) {
member = ((Member<? super X,?>)a);
break;
}
}
}
// TODO check for null member
return new PathImpl(this, member, (Class<X>) member.getJavaType());
}
}

View File

@ -34,6 +34,7 @@ import javax.persistence.metamodel.ListAttribute;
import javax.persistence.metamodel.MapAttribute;
import javax.persistence.metamodel.PluralAttribute;
import javax.persistence.metamodel.SetAttribute;
import javax.persistence.metamodel.Type;
import org.apache.openjpa.kernel.exps.AbstractExpressionBuilder;
import org.apache.openjpa.kernel.exps.ExpressionFactory;
@ -43,6 +44,7 @@ import org.apache.openjpa.meta.FieldMetaData;
import org.apache.openjpa.meta.JavaTypes;
import org.apache.openjpa.persistence.meta.Members;
import org.apache.openjpa.persistence.meta.MetamodelImpl;
import org.apache.openjpa.persistence.meta.Members.MapAttributeImpl;
import org.apache.openjpa.persistence.meta.Members.Member;
/**
@ -517,12 +519,19 @@ public abstract class Joins {
public static class MapKey<Z,K> extends PathImpl<Z,K> {
Map<?,K,?> map;
MapAttributeImpl<Z, K, ?> attr;
public MapKey(Map<Z,K,?> joinMap){
super(((MapAttribute<Z, K, ?>)joinMap.getAttribute()).getKeyJavaType());
attr = ((MapAttributeImpl<Z, K, ?>)joinMap.getAttribute());
this.map = joinMap;
}
@Override
public Type<?> getType() {
return attr.getKeyType();
}
/**
* Convert this path to a join expression.
*

View File

@ -88,6 +88,12 @@ public class ParameterExpressionImpl<T> extends ExpressionImpl<T>
throw new InternalException("Can not assign name [" + auto + "] to " + this);
_autoName = auto;
}
public ParameterExpression<T> alias(String alias) {
assignAutoName(alias);
super.alias(alias);
return this;
}
/**
* Raises an internal exception because parameters of CriteriaQuery

View File

@ -239,7 +239,7 @@ public class PathImpl<Z,X> extends ExpressionImpl<X> implements Path<X> {
* further.
*/
public <Y> Path<Y> get(String attName) {
Type<?> type = _member.getType();
Type<?> type = this.getType();
if (type.getPersistenceType() == PersistenceType.BASIC) {
throw new IllegalArgumentException(this + " is a basic path and can not be navigated to " + attName);
}
@ -249,6 +249,10 @@ public class PathImpl<Z,X> extends ExpressionImpl<X> implements Path<X> {
return new PathImpl<X,Y>(this, next, next.getJavaType());
}
public Type<?> getType() {
return _member.getType();
}
/**
* Get the type() expression corresponding to this path.
*/