OPENJPA-1573: Incorrect SQL for HAVING = entity variable

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@923589 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Catalina Wei 2010-03-16 06:11:18 +00:00
parent 9c09bc3728
commit a5ca9aae2d
4 changed files with 61 additions and 23 deletions

View File

@ -189,6 +189,12 @@ public class PCPath
if (_cid)
return;
Action last = _actions == null ? null : (Action) _actions.getLast();
if (last != null && last.op == Action.VAR && ((String)last.data).equals(last.var)) {
_cid = true;
return;
}
// treat it just like a unique variable
Action action = new Action();
action.op = Action.VAR;

View File

@ -35,8 +35,6 @@ import javax.persistence.criteria.SetJoin;
import javax.persistence.criteria.Subquery;
import org.apache.openjpa.jdbc.sql.DerbyDictionary;
import org.apache.openjpa.persistence.criteria.AbstractCriteriaTestCase.QueryDecorator;
import org.apache.openjpa.persistence.embed.Division;
/**
* Tests type-strict version of Criteria API.
@ -658,17 +656,16 @@ public class TestJPQLSubquery extends CriteriaTest {
String jpql = "select c from Customer c left join c.orders o where exists"
+ " (select o2 from c.orders o2 where o2 = o)";
String expectedSQL = "SELECT t0.id, t0.accountNum, t5.id, t5.city, t5.country, t5.county, "
+ "t5.state, t5.street, t6.userid, t6.DTYPE, t6.age, t6.compName, t6.creditRating, t6.name, "
+ "t5.zipCode, t0.balanceOwed, t0.creditRating, t0.filledOrderCount, t0.firstName, t0.lastName, "
String expectedSQL = "SELECT t0.id, t0.accountNum, t4.id, t4.city, t4.country, t4.county, "
+ "t4.state, t4.street, t5.userid, t5.DTYPE, t5.age, t5.compName, t5.creditRating, t5.name, "
+ "t4.zipCode, t0.balanceOwed, t0.creditRating, t0.filledOrderCount, t0.firstName, t0.lastName, "
+ "t0.name, t0.status "
+ "FROM CR_CUST t0 "
+ "LEFT OUTER JOIN CR_ODR t1 ON t0.id = t1.CUSTOMER_ID "
+ "LEFT OUTER JOIN CR_ODR t2 ON t0.id = t2.CUSTOMER_ID "
+ "LEFT OUTER JOIN CR_ADDR t5 ON t0.ADDRESS_ID = t5.id "
+ "LEFT OUTER JOIN CompUser t6 ON t5.id = t6.ADD_ID WHERE (EXISTS ("
+ "SELECT t4.id FROM CR_ODR t3, CR_ODR t4 WHERE (t2.id = t4.id AND t3.id = t4.id) "
+ "AND (t0.id = t3.CUSTOMER_ID)))";
+ "LEFT OUTER JOIN CR_ADDR t4 ON t0.ADDRESS_ID = t4.id "
+ "LEFT OUTER JOIN CompUser t5 ON t4.id = t5.ADD_ID WHERE (EXISTS ("
+ "SELECT t3.id FROM CR_ODR t2, CR_ODR t3 WHERE (t1.id = t3.id AND t2.id = t3.id) "
+ "AND (t0.id = t2.CUSTOMER_ID)))";
executeAndCompareSQL(jpql, expectedSQL);
@ -711,17 +708,16 @@ public class TestJPQLSubquery extends CriteriaTest {
String jpql = "select c from Customer c left join c.orders o where not exists"
+ " (select o2 from c.orders o2 where o2 = o)";
String expectedSQL = "SELECT t0.id, t0.accountNum, t5.id, t5.city, t5.country, t5.county, "
+ "t5.state, t5.street, t6.userid, t6.DTYPE, t6.age, t6.compName, t6.creditRating, t6.name, "
+ "t5.zipCode, t0.balanceOwed, t0.creditRating, t0.filledOrderCount, t0.firstName, t0.lastName, "
String expectedSQL = "SELECT t0.id, t0.accountNum, t4.id, t4.city, t4.country, t4.county, "
+ "t4.state, t4.street, t5.userid, t5.DTYPE, t5.age, t5.compName, t5.creditRating, t5.name, "
+ "t4.zipCode, t0.balanceOwed, t0.creditRating, t0.filledOrderCount, t0.firstName, t0.lastName, "
+ "t0.name, t0.status "
+ "FROM CR_CUST t0 "
+ "LEFT OUTER JOIN CR_ODR t1 ON t0.id = t1.CUSTOMER_ID "
+ "LEFT OUTER JOIN CR_ODR t2 ON t0.id = t2.CUSTOMER_ID "
+ "LEFT OUTER JOIN CR_ADDR t5 ON t0.ADDRESS_ID = t5.id "
+ "LEFT OUTER JOIN CompUser t6 ON t5.id = t6.ADD_ID WHERE (NOT (EXISTS ("
+ "SELECT t4.id FROM CR_ODR t3, CR_ODR t4 WHERE (t2.id = t4.id AND t3.id = t4.id) "
+ "AND (t0.id = t3.CUSTOMER_ID))))";
+ "LEFT OUTER JOIN CR_ADDR t4 ON t0.ADDRESS_ID = t4.id "
+ "LEFT OUTER JOIN CompUser t5 ON t4.id = t5.ADD_ID WHERE (NOT (EXISTS ("
+ "SELECT t3.id FROM CR_ODR t2, CR_ODR t3 WHERE (t1.id = t3.id AND t2.id = t3.id) "
+ "AND (t0.id = t2.CUSTOMER_ID))))";
executeAndCompareSQL(jpql, expectedSQL);

View File

@ -1085,9 +1085,8 @@ public class TestTypesafeCriteria extends CriteriaTest {
public void testValues1() {
String sql = "SELECT t0.name, t2.id, t2.label FROM CR_ITEM t0 "
+ "INNER JOIN CR_ITEM_photos t1 ON t0.id = t1.ITEM_ID "
+ "INNER JOIN CR_ITEM_photos t3 ON t0.id = t3.ITEM_ID "
+ "INNER JOIN CR_PHT t2 ON t1.VALUE_ID = t2.id WHERE " +
"((t3.VALUE_ID = ? OR t3.VALUE_ID = ? OR t3.VALUE_ID = ? OR t3.VALUE_ID = ? OR t3.VALUE_ID = ?) "
"((t1.VALUE_ID = ? OR t1.VALUE_ID = ? OR t1.VALUE_ID = ? OR t1.VALUE_ID = ? OR t1.VALUE_ID = ?) "
+ "AND 0 < (SELECT COUNT(*) FROM CR_ITEM_photos WHERE CR_ITEM_photos.ITEM_ID = t0.id))";
CriteriaQuery<Customer> q = cb.createQuery(Customer.class);
@ -1167,10 +1166,9 @@ public class TestTypesafeCriteria extends CriteriaTest {
+ "INNER JOIN CR_PHT t2 ON t1.VALUE_ID = t2.id WHERE "
+ "(0 = (SELECT COUNT(*) FROM CR_ITEM_photos t3 WHERE "
+ "(t3.VALUE_ID = ? OR t3.VALUE_ID = ? OR t3.VALUE_ID = ? OR t3.VALUE_ID = ? OR t3.VALUE_ID = ?) "
+ "AND (t0.id = t3.ITEM_ID) AND t0.id = t3.ITEM_ID) "
+ "AND t0.id = t1.ITEM_ID) "
+ "AND 0 < (SELECT COUNT(*) FROM CR_ITEM_photos WHERE CR_ITEM_photos.ITEM_ID = t0.id))";
CriteriaQuery<Customer> q = cb.createQuery(Customer.class);
Root<Item> item = q.from(Item.class);
MapJoin<Item, String, Photo> photo = item.join(Item_.photos);

View File

@ -65,6 +65,44 @@ public class TestSpec10_1_26 extends SQLListenerTestCase {
rsAllDepartment3 = getAll(Department3.class);
}
public void testHavingClauseWithEntityExpression() throws Exception {
EntityManager em = emf.createEntityManager();
Employee1 e1 = em.find(Employee1.class, 1);
em.clear();
String query = "select e from Department1 d, " +
" in (d.empMap) e " +
"group by e " +
"having e = ?1";
Query q = em.createQuery(query);
q.setParameter(1, e1);
List<Employee1> rs = (List<Employee1>) q.getResultList();
Employee1 e2 = rs.get(0);
assertEquals(e1.getEmpId(), e2.getEmpId());
em.clear();
query = "select e from Department1 d, " +
" in (d.empMap) e " +
"group by e " +
"having e <> ?1";
q = em.createQuery(query);
q.setParameter(1, e1);
rs = (List<Employee1>) q.getResultList();
Employee1 e3 = rs.get(0);
assertNotEquals(e1.getEmpId(), e3.getEmpId());
em.clear();
query = "select value(e) from Department1 d, " +
" in (d.empMap) e " +
"group by value(e) " +
"having value(e) = ?1";
q = em.createQuery(query);
q.setParameter(1, e1);
rs = (List<Employee1>) q.getResultList();
Employee1 e4 = rs.get(0);
assertEquals(e1.getEmpId(), e4.getEmpId());
em.close();
}
@AllowFailure
public void testQueryInMemoryQualifiedId() throws Exception {
queryQualifiedId(true);