openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fayw...@apache.org
Subject svn commit: r804013 - in /openjpa/trunk: openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/ openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/
Date Thu, 13 Aug 2009 20:02:43 GMT
Author: faywang
Date: Thu Aug 13 20:02:42 2009
New Revision: 804013

URL: http://svn.apache.org/viewvc?rev=804013&view=rev
Log:
OPENJPA-1185: criteria embeddable subquery support

Modified:
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/AbstractCriteriaTestCase.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestEmbeddableCriteria.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestJPQLSubquery.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestTypesafeCriteria.java
    openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/CriteriaExpressionBuilder.java
    openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/CriteriaQueryImpl.java
    openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/Joins.java

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/AbstractCriteriaTestCase.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/AbstractCriteriaTestCase.java?rev=804013&r1=804012&r2=804013&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/AbstractCriteriaTestCase.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/AbstractCriteriaTestCase.java
Thu Aug 13 20:02:42 2009
@@ -92,8 +92,15 @@
     /**
      * Executes the given CriteriaQuery and JPQL string and compare their respective SQLs
for equality.
      */
+    void assertEquivalence(CriteriaQuery<?> c, String jpql, String expectedSQL) {
+        assertEquivalence(c, jpql, null, null, expectedSQL);
+    }
+
+    /**
+     * Executes the given CriteriaQuery and JPQL string and compare their respective SQLs
for equality.
+     */
     void assertEquivalence(CriteriaQuery<?> c, String jpql) {
-        assertEquivalence(c, jpql, null);
+        assertEquivalence(c, jpql, null, null, null);
     }
 
     /**
@@ -101,12 +108,21 @@
      * supplied parameters, if any.
      */
     void assertEquivalence(CriteriaQuery<?> c, String jpql, String[] paramNames, Object[]
params) {
+        assertEquivalence(c, jpql, paramNames, params, null);
+    }
+    
+    /**
+     * Executes the given CriteriaQuery and JPQL string and compare their respective SQLs
for equality. Sets the
+     * supplied parameters, if any.
+     */
+    void assertEquivalence(CriteriaQuery<?> c, String jpql, String[] paramNames, Object[]
params,
+        String expectedSQL) {
         Query cQ = getEntityManager().createQuery(c);
         Query jQ = getEntityManager().createQuery(jpql);
         setParameters(cQ, paramNames, params);
         setParameters(jQ, paramNames, params);
 
-        executeAndCompareSQL(jpql, cQ, jQ);
+        executeAndCompareSQL(jpql, cQ, jQ, expectedSQL);
     }
 
     /**
@@ -118,7 +134,7 @@
         setParameters(cQ, params);
         setParameters(jQ, params);
 
-        executeAndCompareSQL(jpql, cQ, jQ);
+        executeAndCompareSQL(jpql, cQ, jQ, null);
     }
 
     /**
@@ -129,7 +145,7 @@
      *            The target SQL for the queries will be filled-in the given array.
      * @return true if both queries execute successfully.
      */
-    void executeAndCompareSQL(String jpql, Query cQ, Query jQ) {
+    void executeAndCompareSQL(String jpql, Query cQ, Query jQ, String expectedSQL) {
         List<String> jSQL = null;
         List<String> cSQL = null;
         try {
@@ -163,6 +179,12 @@
                     .get(i));
             }
         }
+        
+        if (expectedSQL != null) {
+            assertEquals("SQL for JPQL and ExpectedSQL for " + jpql + " is different", jSQL.get(0),

+                    expectedSQL);
+            
+        }
     }
 
     void executeAndCompareSQL(String jpql, String expectedSQL) {

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestEmbeddableCriteria.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestEmbeddableCriteria.java?rev=804013&r1=804012&r2=804013&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestEmbeddableCriteria.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestEmbeddableCriteria.java
Thu Aug 13 20:02:42 2009
@@ -744,7 +744,6 @@
         assertEquivalence(q, jpql);
     }
     
-    // JPQL is not correct
     public void testEmbeddableQuery58() {
         String jpql = "select e, a0.intVal2 from EntityA_Embed_Coll_Integer a " +
                 "JOIN a.embed a0 JOIN a0.otherIntVals e WHERE exists (select a from " +
@@ -763,15 +762,7 @@
         sq.where(cb.gt(e1, 0));
         q.where(cb.exists(sq));
         q.orderBy(cb.asc(e));
-        
-        String expectedSQL = "SELECT t1.element, t0.intVal2 " + 
-        "FROM TBL5A t0 INNER JOIN TBL5A_otherIntVals t1 ON t0.id = t1.EMBED_COLL_INTEGER_ID
WHERE (EXISTS (" + 
-        "SELECT t2.id FROM TBL5A t2 INNER JOIN TBL5A_otherIntVals t3 ON t2.id = t3.EMBED_COLL_INTEGER_ID
WHERE " + 
-        "(t3.element > ? AND 1 = 1)) AND 1 = 1) ORDER BY t1.element ASC";
-
-        executeAndCompareSQL(q, expectedSQL);
-        
-        //assertEquivalence(q, jpql);
+        assertEquivalence(q, jpql);
     }
 
     public void testEmbeddableQuery59() {
@@ -810,8 +801,8 @@
     }
     
     //comparison of embed is not support
-    
-    public void btestEmbeddableQuery63() {
+    @AllowFailure(message="")
+    public void testEmbeddableQuery63() {
         String jpql = "select a.embed from EntityA_Embed_Embed a WHERE a.embed.embed IS NOT
NULL";
         CriteriaQuery<Embed_Embed> q = cb.createQuery(Embed_Embed.class);
         Root<EntityA_Embed_Embed> a = q.from(EntityA_Embed_Embed.class);
@@ -832,7 +823,8 @@
         sq.where(cb.isNotNull(a1.get(EntityA_Embed_Embed_.embed)));
         sq.select(a1.get(EntityA_Embed_Embed_.embed).get(Embed_Embed_.embed));
         q.where(cb.exists(sq));
-    }
+        executeExpectFail(q, jpql);
+        executeExpectFail(jpql);    }
     
     public void testEmbeddableQuery65() {
         String jpql = "select a from EntityA_Embed_Embed a";
@@ -977,12 +969,19 @@
         q.where(cb.gt(e.get(Embed_Embed_.intVal1), cb.all(sq)));
         assertEquivalence(q, jpql);
     }
-    
-    // JPQL generates incorrect correlated SQL
+
     public void testEmbeddableQuery76() {
         String jpql = "select e, e.intVal1, e.embed.intVal2 from EntityA_Coll_Embed_Embed
a " +
                 " , in (a.embeds) e WHERE e.intVal1 < ANY (select e.intVal2 " +
                 " from EntityA_Coll_Embed_Embed a, in (a.embeds) e) order by e.intVal3";
+
+        String expectedSQL = "SELECT t1.IntVal1x, t1.IntVal2x, t1.IntVal3x, t1.intVal1, t1.intVal2,
t1.intVal3 " + 
+        "FROM TBL1A t0 INNER JOIN TBL1A_embeds t1 ON t0.id = t1.ENTITYA_COLL_EMBED_EMBED_ID
WHERE " + 
+        "(t1.intVal1 < ANY (" + 
+        "SELECT t3.intVal2 FROM TBL1A t2 " +
+        "INNER JOIN TBL1A_embeds t3 ON t2.id = t3.ENTITYA_COLL_EMBED_EMBED_ID)) " + 
+        "ORDER BY t1.intVal3 ASC";
+
         CriteriaQuery<?> q = cb.createQuery();
         Root<EntityA_Coll_Embed_Embed> a = q.from(EntityA_Coll_Embed_Embed.class);
         ListJoin<EntityA_Coll_Embed_Embed, Embed_Embed> e = a.join(EntityA_Coll_Embed_Embed_.embeds);
@@ -994,21 +993,21 @@
         sq.select(e2.get(Embed_Embed_.intVal2));
         q.where(cb.lt(e.get(Embed_Embed_.intVal1), cb.any(sq)));
         
-        String expectedSQL = "SELECT t1.IntVal1x, t1.IntVal2x, t1.IntVal3x, t1.intVal1, t1.intVal2,
t1.intVal3 " + 
-            "FROM TBL1A t0 INNER JOIN TBL1A_embeds t1 ON t0.id = t1.ENTITYA_COLL_EMBED_EMBED_ID
WHERE " + 
-            "(t1.intVal1 < ANY (" + 
-            "SELECT t3.intVal2 FROM TBL1A t2 " +
-            "INNER JOIN TBL1A_embeds t3 ON t2.id = t3.ENTITYA_COLL_EMBED_EMBED_ID)) " + 
-            "ORDER BY t1.intVal3 ASC";
-        
-        executeAndCompareSQL(q, expectedSQL);
+        assertEquivalence(q, jpql, expectedSQL);
     }
-    
-    // JPQL generates incorrect correlated SQL
+
     public void testEmbeddableQuery77() {
         String jpql = "select e, e.intVal1, e.embed.intVal2 from EntityA_Coll_Embed_Embed
a " +
                 " , in (a.embeds) e WHERE e.intVal1 < ALL (select e.intVal2 " +
                 " from EntityA_Coll_Embed_Embed a, in (a.embeds) e) order by e.intVal3";
+
+        String expectedSQL = "SELECT t1.IntVal1x, t1.IntVal2x, t1.IntVal3x, t1.intVal1, t1.intVal2,
t1.intVal3 " + 
+        "FROM TBL1A t0 INNER JOIN TBL1A_embeds t1 ON t0.id = t1.ENTITYA_COLL_EMBED_EMBED_ID
" +
+        "WHERE (t1.intVal1 < ALL (" + 
+        "SELECT t3.intVal2 FROM TBL1A t2 INNER JOIN TBL1A_embeds t3 " +
+        "ON t2.id = t3.ENTITYA_COLL_EMBED_EMBED_ID)) " + 
+        "ORDER BY t1.intVal3 ASC";
+
         CriteriaQuery<?> q = cb.createQuery();
         Root<EntityA_Coll_Embed_Embed> a = q.from(EntityA_Coll_Embed_Embed.class);
         ListJoin<EntityA_Coll_Embed_Embed, Embed_Embed> e = a.join(EntityA_Coll_Embed_Embed_.embeds);
@@ -1019,16 +1018,7 @@
         Join<EntityA_Coll_Embed_Embed, Embed_Embed> e2 = a1.join(EntityA_Coll_Embed_Embed_.embeds);
         sq.select(e2.get(Embed_Embed_.intVal2));
         q.where(cb.lt(e.get(Embed_Embed_.intVal1), cb.all(sq)));
-        //assertEquivalence(q, jpql);
-        
-        String expectedSQL = "SELECT t1.IntVal1x, t1.IntVal2x, t1.IntVal3x, t1.intVal1, t1.intVal2,
t1.intVal3 " + 
-            "FROM TBL1A t0 INNER JOIN TBL1A_embeds t1 ON t0.id = t1.ENTITYA_COLL_EMBED_EMBED_ID
WHERE "  +
-            "(t1.intVal1 < ALL (" + 
-            "SELECT t3.intVal2 FROM TBL1A t2 " +
-            "INNER JOIN TBL1A_embeds t3 ON t2.id = t3.ENTITYA_COLL_EMBED_EMBED_ID)) " + 
-            "ORDER BY t1.intVal3 ASC";
-        executeAndCompareSQL(q, expectedSQL);
-        
+        assertEquivalence(q, jpql, expectedSQL);
     }
     
     public void testEmbeddableQuery78() {
@@ -1036,6 +1026,11 @@
                 " EntityA_Coll_Embed_Embed a, in (a.embeds) e WHERE e.intVal1 <= SOME
" +
                 " (select e.intVal2 from EntityA_Coll_Embed_Embed a, in (a.embeds) e) " +
                 " order by e.intVal3";
+        String expectedSQL = "SELECT t1.IntVal1x, t1.IntVal2x, t1.IntVal3x, t1.intVal1, t1.intVal2,
t1.intVal3 " + 
+        "FROM TBL1A t0 INNER JOIN TBL1A_embeds t1 ON t0.id = t1.ENTITYA_COLL_EMBED_EMBED_ID
" +
+        "WHERE (t1.intVal1 <= ANY (" + 
+        "SELECT t3.intVal2 FROM TBL1A t2 INNER JOIN TBL1A_embeds t3 ON t2.id = t3.ENTITYA_COLL_EMBED_EMBED_ID))
" + 
+        "ORDER BY t1.intVal3 ASC";
         CriteriaQuery<?> q = cb.createQuery();
         Root<EntityA_Coll_Embed_Embed> a = q.from(EntityA_Coll_Embed_Embed.class);
         ListJoin<EntityA_Coll_Embed_Embed, Embed_Embed> e = a.join(EntityA_Coll_Embed_Embed_.embeds);
@@ -1046,20 +1041,18 @@
         Join<EntityA_Coll_Embed_Embed, Embed_Embed> e2 = a1.join(EntityA_Coll_Embed_Embed_.embeds);
         sq.select(e2.get(Embed_Embed_.intVal2));
         q.where(cb.le(e.get(Embed_Embed_.intVal1), cb.some(sq)));
-        //assertEquivalence(q, jpql);
-        String expectedSQL = "SELECT t1.IntVal1x, t1.IntVal2x, t1.IntVal3x, t1.intVal1, t1.intVal2,
t1.intVal3 " + 
-        "FROM TBL1A t0 INNER JOIN TBL1A_embeds t1 ON t0.id = t1.ENTITYA_COLL_EMBED_EMBED_ID
" +
-        "WHERE (t1.intVal1 <= ANY (" + 
-        "SELECT t3.intVal2 FROM TBL1A t2 INNER JOIN TBL1A_embeds t3 ON t2.id = t3.ENTITYA_COLL_EMBED_EMBED_ID))
" + 
-        "ORDER BY t1.intVal3 ASC";
-    
-        executeAndCompareSQL(q, expectedSQL);
+        assertEquivalence(q, jpql, expectedSQL);
     }
     
     public void testEmbeddableQuery79() {
         String jpql = "select e, e.intVal1, e.embed.intVal2 from EntityA_Coll_Embed_Embed
a " +
                 " , in (a.embeds) e WHERE e.intVal1 > ALL (select e.intVal2 " +
                 " from EntityA_Coll_Embed_Embed a, in (a.embeds) e) order by e.intVal3";
+        String expectedSQL = "SELECT t1.IntVal1x, t1.IntVal2x, t1.IntVal3x, t1.intVal1, t1.intVal2,
t1.intVal3 " + 
+            "FROM TBL1A t0 INNER JOIN TBL1A_embeds t1 ON t0.id = t1.ENTITYA_COLL_EMBED_EMBED_ID
" +
+            "WHERE (t1.intVal1 > ALL (" + 
+            "SELECT t3.intVal2 FROM TBL1A t2 INNER JOIN TBL1A_embeds t3 ON t2.id = t3.ENTITYA_COLL_EMBED_EMBED_ID))
" + 
+            "ORDER BY t1.intVal3 ASC";
         CriteriaQuery<?> q = cb.createQuery();
         Root<EntityA_Coll_Embed_Embed> a = q.from(EntityA_Coll_Embed_Embed.class);
         ListJoin<EntityA_Coll_Embed_Embed, Embed_Embed> e = a.join(EntityA_Coll_Embed_Embed_.embeds);
@@ -1070,20 +1063,18 @@
         Join<EntityA_Coll_Embed_Embed, Embed_Embed> e2 = a1.join(EntityA_Coll_Embed_Embed_.embeds);
         sq.select(e2.get(Embed_Embed_.intVal2));
         q.where(cb.gt(e.get(Embed_Embed_.intVal1), cb.all(sq)));
-        //assertEquivalence(q, jpql);
-        String expectedSQL = "SELECT t1.IntVal1x, t1.IntVal2x, t1.IntVal3x, t1.intVal1, t1.intVal2,
t1.intVal3 " + 
-        "FROM TBL1A t0 INNER JOIN TBL1A_embeds t1 ON t0.id = t1.ENTITYA_COLL_EMBED_EMBED_ID
WHERE " + 
-        "(t1.intVal1 > ALL (" + 
-        "SELECT t3.intVal2 FROM TBL1A t2 INNER JOIN TBL1A_embeds t3 ON t2.id = t3.ENTITYA_COLL_EMBED_EMBED_ID))
" + 
-        "ORDER BY t1.intVal3 ASC";
-    
-        executeAndCompareSQL(q, expectedSQL);
+        assertEquivalence(q, jpql, expectedSQL);
     }
     
     public void testEmbeddableQuery80() {
         String jpql = "select e, e.intVal1, e.embed.intVal2 from EntityA_Coll_Embed_Embed
a " +
                 " , in (a.embeds) e WHERE e.intVal1 < ANY (select e2.intVal2 " +
                 " from in(a.embeds) e2) order by e.intVal3";
+        String expectedSQL = "SELECT t1.IntVal1x, t1.IntVal2x, t1.IntVal3x, t1.intVal1, t1.intVal2,
t1.intVal3 " + 
+        "FROM TBL1A t0 INNER JOIN TBL1A_embeds t1 ON t0.id = t1.ENTITYA_COLL_EMBED_EMBED_ID
WHERE " + 
+        "(t1.intVal1 < ANY (SELECT t2.intVal2 FROM TBL1A_embeds t2 " + 
+        "WHERE (t0.id = t2.ENTITYA_COLL_EMBED_EMBED_ID))) ORDER BY t1.intVal3 ASC";
+
         CriteriaQuery<?> q = cb.createQuery();
         Root<EntityA_Coll_Embed_Embed> a = q.from(EntityA_Coll_Embed_Embed.class);
         ListJoin<EntityA_Coll_Embed_Embed, Embed_Embed> e = a.join(EntityA_Coll_Embed_Embed_.embeds);
@@ -1094,20 +1085,19 @@
         Join<EntityA_Coll_Embed_Embed, Embed_Embed> e2 = a1.join(EntityA_Coll_Embed_Embed_.embeds);
         sq.select(e2.get(Embed_Embed_.intVal2));
         q.where(cb.lt(e.get(Embed_Embed_.intVal1), cb.any(sq)));
-        
-        String expectedSQL = "SELECT t1.IntVal1x, t1.IntVal2x, t1.IntVal3x, t1.intVal1, t1.intVal2,
t1.intVal3 " + 
-        "FROM TBL1A t0 INNER JOIN TBL1A_embeds t1 ON t0.id = t1.ENTITYA_COLL_EMBED_EMBED_ID
WHERE " + 
-        "(t1.intVal1 < ANY (SELECT t2.intVal2 FROM  TBL1A_embeds t2, TBL1A t3 " + 
-        "WHERE (t2.ENTITYA_COLL_EMBED_EMBED_ID = t3.id) AND (t0.id = t2.ENTITYA_COLL_EMBED_EMBED_ID)))
" +
-        "ORDER BY t1.intVal3 ASC";
-        executeAndCompareSQL(q, expectedSQL);
-        //assertEquivalence(q, jpql);
+        assertEquivalence(q, jpql, expectedSQL);
     }
     
     public void testEmbeddableQuery81() {
         String jpql = "select e, e.intVal1, e.embed.intVal2 from EntityA_Coll_Embed_Embed
a " +
                 " , in (a.embeds) e WHERE e.intVal1 < ALL (select e2.intVal2 " +
                 " from a.embeds e2) order by e.intVal3";
+        String expectedSQL = "SELECT t1.IntVal1x, t1.IntVal2x, t1.IntVal3x, t1.intVal1, t1.intVal2,
t1.intVal3 " + 
+        "FROM TBL1A t0 INNER JOIN TBL1A_embeds t1 ON t0.id = t1.ENTITYA_COLL_EMBED_EMBED_ID
WHERE " + 
+        "(t1.intVal1 < ALL (SELECT t2.intVal2 FROM TBL1A_embeds t2 " + 
+        "WHERE (t0.id = t2.ENTITYA_COLL_EMBED_EMBED_ID))) " + 
+        "ORDER BY t1.intVal3 ASC";
+
         CriteriaQuery<?> q = cb.createQuery();
         Root<EntityA_Coll_Embed_Embed> a = q.from(EntityA_Coll_Embed_Embed.class);
         ListJoin<EntityA_Coll_Embed_Embed, Embed_Embed> e = a.join(EntityA_Coll_Embed_Embed_.embeds);
@@ -1118,20 +1108,19 @@
         Join<EntityA_Coll_Embed_Embed, Embed_Embed> e2 = a1.join(EntityA_Coll_Embed_Embed_.embeds);
         sq.select(e2.get(Embed_Embed_.intVal2));
         q.where(cb.lt(e.get(Embed_Embed_.intVal1), cb.all(sq)));
-        //assertEquivalence(q, jpql);
-        
-        String expectedSQL = "SELECT t1.IntVal1x, t1.IntVal2x, t1.IntVal3x, t1.intVal1, t1.intVal2,
t1.intVal3 " + 
-        "FROM TBL1A t0 INNER JOIN TBL1A_embeds t1 ON t0.id = t1.ENTITYA_COLL_EMBED_EMBED_ID
WHERE " + 
-        "(t1.intVal1 < ALL (SELECT t2.intVal2 FROM  TBL1A_embeds t2, TBL1A t3 " + 
-        "WHERE (t2.ENTITYA_COLL_EMBED_EMBED_ID = t3.id) AND (t0.id = t2.ENTITYA_COLL_EMBED_EMBED_ID)))
" + 
-        "ORDER BY t1.intVal3 ASC";
-        executeAndCompareSQL(q, expectedSQL);
+        assertEquivalence(q, jpql, expectedSQL);
     }
     
     public void testEmbeddableQuery82() {
         String jpql = "select e, e.intVal1, e.embed.intVal2 from EntityA_Coll_Embed_Embed
a " +
                 " , in (a.embeds) e WHERE e.intVal1 <= SOME (select e2.intVal2 " +
                 " from in(a.embeds) e2) order by e.intVal3";
+        String expectedSQL = "SELECT t1.IntVal1x, t1.IntVal2x, t1.IntVal3x, t1.intVal1, t1.intVal2,
t1.intVal3 " + 
+        "FROM TBL1A t0 INNER JOIN TBL1A_embeds t1 ON t0.id = t1.ENTITYA_COLL_EMBED_EMBED_ID
WHERE " + 
+        "(t1.intVal1 <= ANY (SELECT t2.intVal2 FROM TBL1A_embeds t2 " + 
+        "WHERE (t0.id = t2.ENTITYA_COLL_EMBED_EMBED_ID))) " + 
+        "ORDER BY t1.intVal3 ASC";
+
         CriteriaQuery<?> q = cb.createQuery();
         Root<EntityA_Coll_Embed_Embed> a = q.from(EntityA_Coll_Embed_Embed.class);
         ListJoin<EntityA_Coll_Embed_Embed, Embed_Embed> e = a.join(EntityA_Coll_Embed_Embed_.embeds);
@@ -1142,20 +1131,19 @@
         Join<EntityA_Coll_Embed_Embed, Embed_Embed> e2 = a1.join(EntityA_Coll_Embed_Embed_.embeds);
         sq.select(e2.get(Embed_Embed_.intVal2));
         q.where(cb.le(e.get(Embed_Embed_.intVal1), cb.some(sq)));
-        //assertEquivalence(q, jpql);
-        
-        String expectedSQL = "SELECT t1.IntVal1x, t1.IntVal2x, t1.IntVal3x, t1.intVal1, t1.intVal2,
t1.intVal3 " + 
-        "FROM TBL1A t0 INNER JOIN TBL1A_embeds t1 ON t0.id = t1.ENTITYA_COLL_EMBED_EMBED_ID
WHERE " + 
-        "(t1.intVal1 <= ANY (SELECT t2.intVal2 FROM  TBL1A_embeds t2, TBL1A t3 " + 
-        "WHERE (t2.ENTITYA_COLL_EMBED_EMBED_ID = t3.id) AND (t0.id = t2.ENTITYA_COLL_EMBED_EMBED_ID)))
" + 
-        "ORDER BY t1.intVal3 ASC";
-        executeAndCompareSQL(q, expectedSQL);
+        assertEquivalence(q, jpql, expectedSQL);
     }
     
     public void testEmbeddableQuery83() {
         String jpql = "select e, e.intVal1, e.embed.intVal2 from EntityA_Coll_Embed_Embed
a " +
                 " , in (a.embeds) e WHERE e.intVal1 > ALL (select e2.intVal2 " +
                 " from a.embeds e2) order by e.intVal3";
+        String expectedSQL = "SELECT t1.IntVal1x, t1.IntVal2x, t1.IntVal3x, t1.intVal1, t1.intVal2,
t1.intVal3 " + 
+        "FROM TBL1A t0 INNER JOIN TBL1A_embeds t1 ON t0.id = t1.ENTITYA_COLL_EMBED_EMBED_ID
WHERE " + 
+        "(t1.intVal1 > ALL (SELECT t2.intVal2 FROM TBL1A_embeds t2 " + 
+        "WHERE (t0.id = t2.ENTITYA_COLL_EMBED_EMBED_ID))) " + 
+        "ORDER BY t1.intVal3 ASC";
+
         CriteriaQuery<?> q = cb.createQuery();
         Root<EntityA_Coll_Embed_Embed> a = q.from(EntityA_Coll_Embed_Embed.class);
         ListJoin<EntityA_Coll_Embed_Embed, Embed_Embed> e = a.join(EntityA_Coll_Embed_Embed_.embeds);
@@ -1166,14 +1154,7 @@
         Join<EntityA_Coll_Embed_Embed, Embed_Embed> e2 = a1.join(EntityA_Coll_Embed_Embed_.embeds);
         sq.select(e2.get(Embed_Embed_.intVal2));
         q.where(cb.gt(e.get(Embed_Embed_.intVal1), cb.all(sq)));
-        //assertEquivalence(q, jpql);
-        
-        String expectedSQL = "SELECT t1.IntVal1x, t1.IntVal2x, t1.IntVal3x, t1.intVal1, t1.intVal2,
t1.intVal3 " + 
-        "FROM TBL1A t0 INNER JOIN TBL1A_embeds t1 ON t0.id = t1.ENTITYA_COLL_EMBED_EMBED_ID
WHERE " + 
-        "(t1.intVal1 > ALL (SELECT t2.intVal2 FROM  TBL1A_embeds t2, TBL1A t3 " + 
-        "WHERE (t2.ENTITYA_COLL_EMBED_EMBED_ID = t3.id) AND (t0.id = t2.ENTITYA_COLL_EMBED_EMBED_ID)))
" + 
-        "ORDER BY t1.intVal3 ASC";
-        executeAndCompareSQL(q, expectedSQL);
+        assertEquivalence(q, jpql, expectedSQL);
     }
     
     @AllowFailure(message="JPQL parse error")
@@ -1281,12 +1262,18 @@
         q.where(cb.isEmpty(a.get(EntityA_Embed_Coll_Embed_.embed).get(Embed_Coll_Embed_.embeds)).negate());
         assertEquivalence(q, jpql);
     }
-    
+
     public void testEmbeddableQuery93() {
+        String jpql1 = "select e, e.intVal1, e.intVal2 from EntityA_Embed_Coll_Embed a "
+
+            " , in (a.embed.embeds) e where exists (select e.intVal1 " +
+            " from EntityA_Embed_Coll_Embed a, in (a.embed.embeds) e " +
+            " where e.intVal2 = 105) order by e";
+        
         String jpql = "select e, e.intVal1, e.intVal2 from EntityA_Embed_Coll_Embed a " +
-                " , in (a.embed.embeds) e where exists (select e.intVal1 " +
-                " from EntityA_Embed_Coll_Embed a, in (a.embed.embeds) e " +
-                " where e.intVal2 = 105) order by e";
+            " JOIN a.embed ae JOIN ae.embeds e where exists (select e.intVal1 " +
+            " from EntityA_Embed_Coll_Embed a JOIN a.embed ae JOIN ae.embeds e " +
+            " where e.intVal2 = 105) order by e";
+
         CriteriaQuery<?> q = cb.createQuery();
         Root<EntityA_Embed_Coll_Embed> a = q.from(EntityA_Embed_Coll_Embed.class);
         ListJoin<Embed_Coll_Embed, Embed> e = a.join(EntityA_Embed_Coll_Embed_.embed).join(Embed_Coll_Embed_.embeds);
@@ -1298,15 +1285,7 @@
         sq.where(cb.equal(e1.get(Embed_.intVal2), 105));
         sq.select(e1.get(Embed_.intVal1));
         q.where(cb.exists(sq));
-        //assertEquivalence(q, jpql);
-        
-        String expectedSQL = "SELECT t3.A1, t3.A2, t3.A3 " + 
-        "FROM TBL4A t2 INNER JOIN TBL4A_embeds t3 ON t2.id = t3.EMBED_COLL_EMBED_ID WHERE
(" + 
-        "EXISTS (SELECT t1.A1 FROM TBL4A t0 INNER JOIN TBL4A_embeds t1 ON t0.id = t1.EMBED_COLL_EMBED_ID
" + 
-        "WHERE (t1.A2 = ? AND 1 = 1)) AND 1 = 1) ORDER BY t3.A1 ASC, t3.A2 ASC, t3.A3 ASC";
-
-        executeAndCompareSQL(q, expectedSQL);
-
+        assertEquivalence(q, jpql);
     }
     
     public void testEmbeddableQuery94() {
@@ -1328,9 +1307,6 @@
         sq.select(e2.get(Embed_.intVal1));
         q.where(cb.equal(e.get(Embed_.intVal1), cb.some(sq)));
         assertEquivalence(q, jpql);
-        //String expectedSQL = "";
-    
-        //executeAndCompareSQL(q, expectedSQL);
     }
     
     public void testEmbeddableQuery95() {
@@ -1413,6 +1389,7 @@
         assertEquivalence(q, jpql);
     }
     
+    @AllowFailure(message = "JPQL has redundant table join")
     public void testEmbeddableQuery103() {
         String jpql = "select d from Department1 d where d.deptId < ANY " +
                 " (select KEY(e) from in(d.empMap) e) order by d";
@@ -1665,31 +1642,31 @@
     }
     
     public void testEmbeddableQuery122() {
-        String jpql = "select c from Company1 c where exists (select d from in(c.organization)
d" +
+        String jpql = "select c from Company1 c where exists (select KEY(d) from in(c.organization)
d" +
                 "  where KEY(d) = :division) order by c ";
         CriteriaQuery<Company1> q = cb.createQuery(Company1.class);
         Root<Company1> c = q.from(Company1.class);
-        Subquery<Map> sq = q.subquery(Map.class);
+        Subquery<Division> sq = q.subquery(Division.class);
         Root<Company1> c1 = sq.correlate(c);
         MapJoin<Company1,Division,VicePresident> d = c1.join(Company1_.organization);
-        //sq.select(d);
         q.select(c);
         Parameter<Division> param1 = cb.parameter(Division.class, "division");
         sq.where(cb.equal(d.key(), param1));
+        sq.select(d.key());
         q.where(cb.exists(sq));
         q.orderBy(cb.asc(c));
         assertEquivalence(q, jpql, new String[]{"division"}, new Object[]{new Division()});
     }
     
     public void testEmbeddableQuery123() {
-        String jpql = "select c from Company1 c where exists (select d from c.organization
d" +
+        String jpql = "select c from Company1 c where exists (select KEY(d) from c.organization
d" +
                 "  where KEY(d) = :division) order by c ";  
         CriteriaQuery<Company1> q = cb.createQuery(Company1.class);
         Root<Company1> c = q.from(Company1.class);
-        Subquery<Map> sq = q.subquery(Map.class);
+        Subquery<Division> sq = q.subquery(Division.class);
         Root<Company1> c1 = sq.correlate(c);
         MapJoin<Company1,Division,VicePresident> d = c1.join(Company1_.organization);
-        //sq.select(d);
+        sq.select(d.key());
         q.select(c);
         Parameter<Division> param1 = cb.parameter(Division.class, "division");
         sq.where(cb.equal(d.key(), param1));
@@ -1698,7 +1675,6 @@
         assertEquivalence(q, jpql, new String[]{"division"}, new Object[]{new Division()});
     }
     
-    @AllowFailure(message="RelationRelationMapTableFieldStrategy.toKeyDataStoreValue() fails.
Analyze MapKey")
     public void testEmbeddableQuery124() {
         String jpql = "select c from Company2 c where :division = (select KEY(d) from Company2
c, in(c.organization) d" 
                     + " where d.id = 3) order by c ";
@@ -1710,14 +1686,14 @@
         sq.select(d.key());
         q.select(c);
         ParameterExpression<Division> param1 = cb.parameter(Division.class, "division");
-        sq.where(cb.equal(d.key().get(Division_.id), 3));
+        sq.where(cb.equal(d.value().get(VicePresident_.id), 3));
         q.where(cb.equal(param1, sq));
         q.orderBy(cb.asc(c));
         assertEquivalence(q, jpql, new String[]{"division"}, new Object[]{new Division()});
     }
     
     public void testEmbeddableQuery125() {
-        String jpql = "select c from Company2 c where exists (select d from in(c.organization)
d" +
+        String jpql = "select c from Company2 c where exists (select KEY(d) from in(c.organization)
d" +
                 "  where KEY(d) = :k) order by c ";
         CriteriaQuery<Company2> q = cb.createQuery(Company2.class);
         Root<Company2> c = q.from(Company2.class);
@@ -1727,22 +1703,24 @@
         q.select(c);
         Parameter<Division> param1 = cb.parameter(Division.class, "k");
         sq.where(cb.equal(d.key(), param1));
+        sq.select(d.key());
         q.where(cb.exists(sq));
         q.orderBy(cb.asc(c));
         assertEquivalence(q, jpql, new String[]{"k"}, new Object[]{new Division()});
     }
     
     public void testEmbeddableQuery126() {
-        String jpql = "select c from Company2 c where exists (select d from c.organization
d  where KEY(d) = :k) " 
-                    + "order by c ";  
+        String jpql = "select c from Company2 c where exists (select VALUE(d) from c.organization
d "
+            + "where KEY(d) = :k) order by c ";  
         CriteriaQuery<Company2> q = cb.createQuery(Company2.class);
         Root<Company2> c = q.from(Company2.class);
-        Subquery<Division> sq = q.subquery(Division.class);
+        Subquery<VicePresident> sq = q.subquery(VicePresident.class);
         Root<Company2> c1 = sq.correlate(c);
         MapJoin<Company2,Division,VicePresident> d = c1.join(Company2_.organization);
         q.select(c);
         Parameter<Division> param1 = cb.parameter(Division.class, "k");
         sq.where(cb.equal(d.key(), param1));
+        sq.select(d.value());
         q.where(cb.exists(sq));
         q.orderBy(cb.asc(c));
         assertEquivalence(q, jpql, new String[]{"k"}, new Object[]{new Division()});

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestJPQLSubquery.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestJPQLSubquery.java?rev=804013&r1=804012&r2=804013&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestJPQLSubquery.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestJPQLSubquery.java
Thu Aug 13 20:02:42 2009
@@ -146,7 +146,7 @@
             + "t0.filledOrderCount, t0.firstName, t0.lastName, t0.name, t0.status "
             + "FROM CR_CUST t0 LEFT OUTER JOIN CR_ADDR t3 ON t0.ADDRESS_ID = t3.id "
             + "LEFT OUTER JOIN CompUser t4 ON t3.id = t4.ADD_ID "
-            + "WHERE ((SELECT COUNT(t2.id) FROM  CR_ODR t1, CR_ODR t2 WHERE (t1.id = t2.id)
AND "
+            + "WHERE ((SELECT COUNT(t2.id) FROM CR_ODR t1, CR_ODR t2 WHERE (t1.id = t2.id)
AND "
             + "(t0.id = t1.CUSTOMER_ID)) > ?)";
 
         executeAndCompareSQL(jpql, expectedSQL);
@@ -364,7 +364,7 @@
             + "LEFT OUTER JOIN CR_CUST t4 ON t0.CUSTOMER_ID = t4.id "
             + "LEFT OUTER JOIN CR_ADDR t5 ON t4.ADDRESS_ID = t5.id "
             + "LEFT OUTER JOIN CompUser t6 ON t5.id = t6.ADD_ID WHERE (? < ALL ("
-            + "SELECT t3.balance FROM  CR_CUST_CR_ACCT t2, CR_ACCT t3 WHERE (t2.ACCOUNTS_ID
= t3.id) AND "
+            + "SELECT t3.balance FROM CR_CUST_CR_ACCT t2, CR_ACCT t3 WHERE (t2.ACCOUNTS_ID
= t3.id) AND "
             + "(t1.id = t2.CUSTOMER_ID)))";
 
         executeAndCompareSQL(jpql, expectedSQL);
@@ -393,7 +393,7 @@
             + "LEFT OUTER JOIN CR_CUST t4 ON t0.CUSTOMER_ID = t4.id "
             + "LEFT OUTER JOIN CR_ADDR t5 ON t4.ADDRESS_ID = t5.id "
             + "LEFT OUTER JOIN CompUser t6 ON t5.id = t6.ADD_ID WHERE (t0.name = ANY ("
-            + "SELECT t3.name FROM  CR_CUST_CR_ACCT t2, CR_ACCT t3 WHERE (t2.ACCOUNTS_ID
= t3.id) "
+            + "SELECT t3.name FROM CR_CUST_CR_ACCT t2, CR_ACCT t3 WHERE (t2.ACCOUNTS_ID =
t3.id) "
             + "AND (t1.id = t2.CUSTOMER_ID)))";
 
         executeAndCompareSQL(jpql, expectedSQL);
@@ -516,7 +516,7 @@
             + "INNER JOIN CR_ADDR t2 ON t1.ADDRESS_ID = t2.id "
             + "LEFT OUTER JOIN CR_ADDR t6 ON t5.ADDRESS_ID = t6.id "
             + "LEFT OUTER JOIN CompUser t7 ON t6.id = t7.ADD_ID "
-            + "WHERE (? < ALL (SELECT t4.age FROM  CompUser t3, CompUser t4 "
+            + "WHERE (? < ALL (SELECT t4.age FROM CompUser t3, CompUser t4 "
             + "WHERE (t3.userid = t4.userid) AND (t2.id = t3.ADD_ID)) AND 1 = 1)";
 
         executeAndCompareSQL(jpql, expectedSQL);
@@ -599,7 +599,7 @@
             + "INNER JOIN CR_ADDR t2 ON t1.ADDRESS_ID = t2.id "
             + "LEFT OUTER JOIN CR_ADDR t6 ON t1.ADDRESS_ID = t6.id "
             + "LEFT OUTER JOIN CompUser t7 ON t6.id = t7.ADD_ID WHERE (? < ALL ("
-            + "SELECT t5.age FROM  CR_CUST t3,  CompUser t4, CompUser t5 "
+            + "SELECT t5.age FROM CR_CUST t3, CompUser t4, CompUser t5 "
             + "WHERE (t4.userid = t5.userid) AND (t0.CUSTOMER_ID = t3.id) AND (t2.id = t4.ADD_ID)))";
 
         executeAndCompareSQL(jpql, expectedSQL);
@@ -665,7 +665,7 @@
             + "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)
"
+            + "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)))";
 
         executeAndCompareSQL(jpql, expectedSQL);
@@ -718,7 +718,7 @@
             + "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)
"
+            + "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))))";
 
         executeAndCompareSQL(jpql, expectedSQL);
@@ -856,7 +856,7 @@
             + " (select count(i) from o.lineItems i)";
 
         String expectedSQL = "SELECT t0.id FROM CR_ODR t0 WHERE (t0.quantity > ("
-            + "SELECT COUNT(t2.id) FROM  CR_LI t1, CR_LI t2 "
+            + "SELECT COUNT(t2.id) FROM CR_LI t1, CR_LI t2 "
             + "WHERE (t1.id = t2.id) AND (t0.id = t1.ORDER_ID)))";
 
         executeAndCompareSQL(jpql, expectedSQL);
@@ -941,9 +941,9 @@
             + "or exists (select o from c.orders o where o.id = 2)";
 
         String expectedSQL = "SELECT t0.name FROM CR_CUST t0 WHERE (EXISTS ("
-            + "SELECT t2.id FROM  CR_ODR t1, CR_ODR t2 "
+            + "SELECT t2.id FROM CR_ODR t1, CR_ODR t2 "
             + "WHERE (t2.id = ? AND t1.id = t2.id) AND (t0.id = t1.CUSTOMER_ID)) OR EXISTS
("
-            + "SELECT t4.id FROM  CR_ODR t3, CR_ODR t4 "
+            + "SELECT t4.id FROM CR_ODR t3, CR_ODR t4 "
             + "WHERE (t4.id = ? AND t3.id = t4.id) AND (t0.id = t3.CUSTOMER_ID)))";
         executeAndCompareSQL(jpql, expectedSQL);
 
@@ -1061,7 +1061,7 @@
 
         String expectedSQL = "SELECT t1.id FROM CR_CUST t0 "
             + "INNER JOIN CR_ODR t1 ON t0.id = t1.CUSTOMER_ID WHERE (t1.quantity > ("
-            + "SELECT SUM(t3.quantity) FROM  CR_ODR t2, CR_ODR t3 "
+            + "SELECT SUM(t3.quantity) FROM CR_ODR t2, CR_ODR t3 "
             + "WHERE (t2.id = t3.id) AND (t0.id = t2.CUSTOMER_ID)))";
         executeAndCompareSQL(jpql, expectedSQL);
 
@@ -1087,7 +1087,7 @@
 
         String expectedSQL = "SELECT t0.id, t1.name " + 
         "FROM CR_ODR t0 JOIN CR_CUST t1 ON (1 = 1) WHERE (t0.quantity = ANY (" + 
-        "SELECT t3.quantity FROM  CR_ODR t2, CR_ODR t3 WHERE (t2.id = t3.id) AND (t1.id =
t2.CUSTOMER_ID)))"; 
+        "SELECT t3.quantity FROM CR_ODR t2, CR_ODR t3 WHERE (t2.id = t3.id) AND (t1.id =
t2.CUSTOMER_ID)))"; 
 
         executeAndCompareSQL(jpql, expectedSQL);
 
@@ -1236,7 +1236,7 @@
             + " (select CURRENT_TIMESTAMP from o.lineItems i)";
 
         String expectedSQL = "SELECT t0.id FROM CR_ODR t0 WHERE (t0.orderTs > ("
-            + "SELECT CURRENT_TIMESTAMP FROM  CR_LI t1, CR_LI t2 WHERE (t1.id = t2.id) AND
"
+            + "SELECT CURRENT_TIMESTAMP FROM CR_LI t1, CR_LI t2 WHERE (t1.id = t2.id) AND
"
             + "(t0.id = t1.ORDER_ID)))";
         executeAndCompareSQL(jpql, expectedSQL);
 

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestTypesafeCriteria.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestTypesafeCriteria.java?rev=804013&r1=804012&r2=804013&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestTypesafeCriteria.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestTypesafeCriteria.java
Thu Aug 13 20:02:42 2009
@@ -1140,8 +1140,9 @@
             + "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 0 < (SELECT COUNT(*) FROM CR_ITEM_photos WHERE
"
-            + "CR_ITEM_photos.ITEM_ID = t0.id))"; 
+            + "AND (t0.id = t3.ITEM_ID) AND t0.id = t3.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);

Modified: openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/CriteriaExpressionBuilder.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/CriteriaExpressionBuilder.java?rev=804013&r1=804012&r2=804013&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/CriteriaExpressionBuilder.java
(original)
+++ openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/CriteriaExpressionBuilder.java
Thu Aug 13 20:02:42 2009
@@ -276,6 +276,13 @@
         List<Value> projections, List<String> aliases, List<String> clauses,

         ExpressionFactory factory, CriteriaQueryImpl<?> q, MetamodelImpl model, 
         Map<ExpressionImpl<?>, Value> exp2Vals) {
+
+        if (selections.size() == 0 && q.getDelegator() != null) { // this is subquery
+            Root<?> r = q.getRoot();
+            selections = new ArrayList<Selection<?>>(1);
+            selections.add(r);
+        }
+        
         for (Selection<?> s : selections) {
             if (s.isCompoundSelection()) {
                 getProjections(exps, s.getCompoundSelectionItems(), projections, aliases,


Modified: openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/CriteriaQueryImpl.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/CriteriaQueryImpl.java?rev=804013&r1=804012&r2=804013&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/CriteriaQueryImpl.java
(original)
+++ openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/CriteriaQueryImpl.java
Thu Aug 13 20:02:42 2009
@@ -22,20 +22,13 @@
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.HashMap;
-import java.util.HashSet;
-import java.util.LinkedHashMap;
 import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
-import java.util.SortedSet;
 import java.util.Stack;
-import java.util.TreeSet;
 
-import javax.persistence.Tuple;
-import javax.persistence.TupleElement;
 import javax.persistence.criteria.AbstractQuery;
-import javax.persistence.criteria.CompoundSelection;
 import javax.persistence.criteria.CriteriaQuery;
 import javax.persistence.criteria.Expression;
 import javax.persistence.criteria.Order;
@@ -47,15 +40,11 @@
 import javax.persistence.metamodel.EntityType;
 
 import org.apache.commons.collections.map.LinkedMap;
-import org.apache.openjpa.kernel.FillStrategy;
-import org.apache.openjpa.kernel.ResultShape;
 import org.apache.openjpa.kernel.StoreQuery;
 import org.apache.openjpa.kernel.exps.Context;
 import org.apache.openjpa.kernel.exps.ExpressionFactory;
 import org.apache.openjpa.kernel.exps.QueryExpressions;
 import org.apache.openjpa.kernel.exps.Value;
-import org.apache.openjpa.persistence.TupleFactory;
-import org.apache.openjpa.persistence.TupleImpl;
 import org.apache.openjpa.persistence.meta.MetamodelImpl;
 import org.apache.openjpa.persistence.meta.Types;
 
@@ -458,8 +447,8 @@
         _variables.put(node, var);
         _values.put(node, path);
         _aliases.put(node, alias);
-        _contexts.peek().addSchema(alias, var.getMetaData());
-        _contexts.peek().addVariable(alias, var);
+        ctx().addSchema(alias, var.getMetaData());
+        ctx().addVariable(alias, var);
     }
     
     public boolean isRegistered(Selection<?> selection) {
@@ -512,6 +501,9 @@
 
     public void registerRoot(Root<?> root, Value var) {
         _rootVariables.put(root, var);
+        String alias = var.getName();
+        ctx().addSchema(alias, var.getMetaData());
+        ctx().addVariable(alias, var);
     }
     
     public Value getRootVariable(Root<?> root) {

Modified: openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/Joins.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/Joins.java?rev=804013&r1=804012&r2=804013&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/Joins.java
(original)
+++ openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/Joins.java
Thu Aug 13 20:02:42 2009
@@ -194,7 +194,7 @@
                 parentPath.get(_member.fmd, allowNull);
                 parentPath.setSchemaAlias(c.getAlias(correlatedParentPath));
                 if (c.ctx().getParent() != null && c.ctx().getVariable(parentPath.getSchemaAlias())
== null) 
-                    parentPath.setSubqueryContext(c.ctx());
+                    parentPath.setSubqueryContext(c.ctx(), parentPath.getSchemaAlias());
                 
                 path.setMetaData(meta);
                 //filter = bindVariableForKeyPath(path, alias, filter);
@@ -343,6 +343,7 @@
                     org.apache.openjpa.kernel.exps.Subquery subQ = subquery.getSubQ();
                     path = factory.newPath(subQ); 
                     if ((corrJoin != null || corrRoot != null) && _parent.getCorrelatedPath()
!= null) { 
+                        subQ.setSubqAlias(c.getAlias(this));
                         path = factory.newPath(subQ);
                         correlatedParentPath = _parent.getCorrelatedPath();
                         bind = false;
@@ -392,18 +393,22 @@
                 parentPath.get(_member.fmd, allowNull);
                 parentPath.setSchemaAlias(c.getAlias(correlatedParentPath));
                 if (c.ctx().getParent() != null && c.ctx().getVariable(parentPath.getSchemaAlias())
== null) 
-                    parentPath.setSubqueryContext(c.ctx());
+                    parentPath.setSubqueryContext(c.ctx(), parentPath.getSchemaAlias());
                 
+                path.setSchemaAlias(c.getAlias(correlatedParentPath));
                 path.setMetaData(meta);
-                //filter = bindVariableForKeyPath(path, alias, filter);
-                filter = factory.equal(parentPath, path);
-                if (_member.fmd.isElementCollection() || _member.fmd.getDeclaredTypeCode()
== JavaTypes.MAP) {
-                    Class<?> type = meta == null ? AbstractExpressionBuilder.TYPE_OBJECT
: meta.getDescribedType(); 
-                    Value var = factory.newBoundVariable(c.getAlias(this), type);
-                    join = factory.bindVariable(var, parentPath);
+                Class<?> type = meta == null ? AbstractExpressionBuilder.TYPE_OBJECT
: meta.getDescribedType(); 
+                Value var = factory.newBoundVariable(c.getAlias(this), type);
+                join = factory.bindVariable(var, parentPath);
+                
+                if (_member.fmd.getDeclaredTypeCode() == JavaTypes.MAP)
                     c.registerVariable(this, var, parentPath);
+                
+                if (_member.fmd.isElementCollection()) 
                     filter = CriteriaExpressionBuilder.and(factory, join, filter);
-                }
+                else 
+                    filter = factory.equal(parentPath, path);
+                
                 return CriteriaExpressionBuilder.and(factory, expr, filter);
             }
         }
@@ -563,15 +568,7 @@
            PathImpl<?,?> parent = map.getInnermostParentPath();
            Value val = c.getRegisteredVariable(map);
            org.apache.openjpa.kernel.exps.Path path = factory.newPath(val);
-           if (parent.inSubquery(subquery)) {
-               org.apache.openjpa.kernel.exps.Subquery subQ = subquery.getSubQ();
-               org.apache.openjpa.kernel.exps.Path var = factory.newPath(subQ);
-               ((org.apache.openjpa.kernel.exps.Path)var).setSchemaAlias(c.getAlias(this));
-               var.setMetaData(subQ.getMetaData());
-               return factory.mapKey(path, var);
-           } else {
-               return factory.getKey(path);
-           }
+           return factory.getKey(path);
        }
    }
        



Mime
View raw message