diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/PCPath.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/PCPath.java index ea59e7836..2cfe5ab65 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/PCPath.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/PCPath.java @@ -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; diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestJPQLSubquery.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestJPQLSubquery.java index 6f1ff04fa..ba77009d3 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestJPQLSubquery.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestJPQLSubquery.java @@ -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); diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestTypesafeCriteria.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestTypesafeCriteria.java index 2ad162969..a19e1a1e9 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestTypesafeCriteria.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestTypesafeCriteria.java @@ -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 = ?) " + + "INNER JOIN CR_PHT t2 ON t1.VALUE_ID = t2.id WHERE " + + "((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 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 q = cb.createQuery(Customer.class); Root item = q.from(Item.class); MapJoin photo = item.join(Item_.photos); diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex0/TestSpec10_1_26.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex0/TestSpec10_1_26.java index b114701b0..0c5de8b2f 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex0/TestSpec10_1_26.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex0/TestSpec10_1_26.java @@ -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 rs = (List) 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) 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) q.getResultList(); + Employee1 e4 = rs.get(0); + assertEquals(e1.getEmpId(), e4.getEmpId()); + em.close(); + } + @AllowFailure public void testQueryInMemoryQualifiedId() throws Exception { queryQualifiedId(true);