openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ppod...@apache.org
Subject svn commit: r785341 [2/3] - in /openjpa/trunk: openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/ openjpa-persistence/src/main/java/org/apache/openjpa/persiste...
Date Tue, 16 Jun 2009 18:56:58 GMT
Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestTypeSafeCondExpression.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestTypeSafeCondExpression.java?rev=785341&r1=785340&r2=785341&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestTypeSafeCondExpression.java (original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestTypeSafeCondExpression.java Tue Jun 16 18:56:57 2009
@@ -36,22 +36,12 @@
  */
 package org.apache.openjpa.persistence.criteria;
 
-import java.sql.Timestamp;
-import java.util.List;
-
-import javax.persistence.EntityManager;
 import javax.persistence.Parameter;
 import javax.persistence.criteria.CriteriaQuery;
 import javax.persistence.criteria.Expression;
-import javax.persistence.criteria.Join;
-import javax.persistence.criteria.JoinType;
-import javax.persistence.criteria.ListJoin;
 import javax.persistence.criteria.Root;
-import javax.persistence.criteria.SetJoin;
 import javax.persistence.criteria.Subquery;
 
-import org.apache.openjpa.persistence.test.AllowFailure;
-
 /**
  * Tests type-strict version of Criteria API. The test scenarios are adapted
  * from TestEJBQLCondExpression in
@@ -70,10 +60,9 @@
         assertEquivalence(c, query);
     }
 
-    public void testBetweenExprUsingCriteria() {
-        String query =
-            "SELECT o.name FROM CompUser o WHERE o.age BETWEEN 19 AND 40 AND " +
-            "o.computerName = 'PC'";
+    public void testBetween() {
+        String jpql = "SELECT o.name FROM CompUser o " 
+                    + "WHERE o.age BETWEEN 19 AND 40 AND o.computerName = 'PC'";
         
         CriteriaQuery cq = cb.create();
         Root<CompUser> o = cq.from(CompUser.class);
@@ -81,112 +70,108 @@
                 cb.equal(o.get(CompUser_.computerName), "PC")));
         cq.select(o.get(CompUser_.name));
         
-        assertEquivalence(cq, query);
+        assertEquivalence(cq, jpql);
     }
 
-    public void testNotBetweenExprUsingCriteria() {
-        String query =
-            "SELECT o.name FROM CompUser o WHERE o.age NOT BETWEEN 19 AND 40 " +
-            "AND o.computerName= 'PC'";
+    public void testNotBetween() {
+        String jpql = "SELECT o.name FROM CompUser o " 
+                 + "WHERE o.age NOT BETWEEN 19 AND 40 AND o.computerName= 'PC'";
 
-        CriteriaQuery q = cb.create();
-        Root<CompUser> c = q.from(CompUser.class);
-        q.where(cb.and(cb.between(c.get(CompUser_.age), 19, 40).negate(), 
-                cb.equal(c.get(CompUser_.computerName), "PC")));
-        q.select(c.get(CompUser_.name));
+        CriteriaQuery cq = cb.create();
+        Root<CompUser> o = cq.from(CompUser.class);
+        cq.where(cb.and(cb.between(o.get(CompUser_.age), 19, 40).negate(), 
+                cb.equal(o.get(CompUser_.computerName), "PC")));
+        cq.select(o.get(CompUser_.name));
         
-        assertEquivalence(q, query);
+        assertEquivalence(cq, jpql);
     }
 
-    public void testInExprUsingCriteria() {
-        String query =
-            "SELECT o.name FROM CompUser o WHERE o.age IN (29, 40, 10)";
-        CriteriaQuery q = cb.create();
-        Root<CompUser> c = q.from(CompUser.class);
-        q.where(cb.in(c.get(CompUser_.age)).value(29).value(40).value(10));
-        q.select(c.get(CompUser_.name));
+    public void testInExpr() {
+        String jpql = "SELECT o.name FROM CompUser o "
+                     + "WHERE o.age IN (29, 40, 10)";
         
-        assertEquivalence(q, query);
+        CriteriaQuery cq = cb.create();
+        Root<CompUser> o = cq.from(CompUser.class);
+        cq.where(cb.in(o.get(CompUser_.age)).value(29).value(40).value(10));
+        cq.select(o.get(CompUser_.name));
+        
+        assertEquivalence(cq, jpql);
     }
 
-    public void testNotInUsingCriteria() {
-        String query =
-            "SELECT o.name FROM CompUser o WHERE o.age NOT IN (29, 40, 10)";
+    public void testNotIn() {
+        String jpql = "SELECT o.name FROM CompUser o "
+                    + "WHERE o.age NOT IN (29, 40, 10)";
 
-        CriteriaQuery q = cb.create();
-        Root<CompUser> c = q.from(CompUser.class);
-        q.where(cb.in(c.get(CompUser_.age)).value(29).value(40).value(10)
+        CriteriaQuery cq = cb.create();
+        Root<CompUser> o = cq.from(CompUser.class);
+        cq.where(cb.in(o.get(CompUser_.age)).value(29).value(40).value(10)
             .negate());
-        q.select(c.get(CompUser_.name));
+        cq.select(o.get(CompUser_.name));
         
-        assertEquivalence(q, query);
+        assertEquivalence(cq, jpql);
     }
 
-    public void testLikeExprUsingCriteria1() {
-        String query =
-            "SELECT o.computerName FROM CompUser o WHERE o.name LIKE 'Sha%'" +
-            " AND o.computerName NOT IN ('PC')";
+    public void testLike1() {
+        String jpql = "SELECT o.computerName FROM CompUser o "
+                 + "WHERE o.name LIKE 'Sha%' AND o.computerName NOT IN ('PC')";
 
-        CriteriaQuery q = cb.create();
-        Root<CompUser> c = q.from(CompUser.class);
-        q.where(cb.and(
-                    cb.like(c.get(CompUser_.name),"Sha%"), 
-                    cb.in(c.get(CompUser_.computerName)).value("PC").negate()
+        CriteriaQuery cq = cb.create();
+        Root<CompUser> o = cq.from(CompUser.class);
+        cq.where(cb.and(
+                    cb.like(o.get(CompUser_.name),"Sha%"), 
+                    cb.in(o.get(CompUser_.computerName)).value("PC").negate()
                 ));
         
-        q.select(c.get(CompUser_.computerName));
+        cq.select(o.get(CompUser_.computerName));
 
-        assertEquivalence(q, query);
+        assertEquivalence(cq, jpql);
     }
     
-    public void testLikeExprUsingCriteria2() {
-        String query =
-            "SELECT o.computerName FROM CompUser o WHERE o.name LIKE 'Sha%o_'" +
-            " AND " + 
-            "o.computerName NOT IN ('UNIX')";
+    public void testLike2() {
+        String jpql = "SELECT o.computerName FROM CompUser o "
+            + "WHERE o.name LIKE 'Sha%o_' AND o.computerName NOT IN ('UNIX')";
 
-        CriteriaQuery q = cb.create();
-        Root<CompUser> c = q.from(CompUser.class);
-        q.where(cb.and(
-                    cb.like(c.get(CompUser_.name),"Sha%o_"), 
-                    cb.in(c.get(CompUser_.computerName)).value("UNIX").negate()
+        CriteriaQuery cq = cb.create();
+        Root<CompUser> o = cq.from(CompUser.class);
+        cq.where(cb.and(
+                    cb.like(o.get(CompUser_.name),"Sha%o_"), 
+                    cb.in(o.get(CompUser_.computerName)).value("UNIX").negate()
                 ));
-        q.select(c.get(CompUser_.computerName));
+        cq.select(o.get(CompUser_.computerName));
         
-        assertEquivalence(q, query);
+        assertEquivalence(cq, jpql);
     }
     
-    public void testLikeExprUsingCriteria3() {
-        String query = "SELECT o.name FROM CompUser o WHERE o.name LIKE '_J%'";
+    public void testLike3() {
+        String jpql = "SELECT o.name FROM CompUser o WHERE o.name LIKE '_J%'";
 
-        CriteriaQuery q = cb.create();
-        Root<CompUser> c = q.from(CompUser.class);
-        q.where(cb.like(c.get(CompUser_.name),"_J%"));
-        q.select(c.get(CompUser_.name));
+        CriteriaQuery cq = cb.create();
+        Root<CompUser> o = cq.from(CompUser.class);
+        cq.where(cb.like(o.get(CompUser_.name),"_J%"));
+        cq.select(o.get(CompUser_.name));
         
-        assertEquivalence(q, query);
+        assertEquivalence(cq, jpql);
     }
     
-    @AllowFailure(message="Parameter processing is broken")
-    public void testLikeExprUsingCriteria4() {
-        String query = "SELECT o.name FROM CompUser o WHERE o.name LIKE ?1 " +
-        		"ESCAPE '|'";
+    public void testLikeWithEscapeCharacter() {
+        String query = "SELECT o.name FROM CompUser o "
+            + "WHERE o.name LIKE :name ESCAPE '|'";
         CriteriaQuery q = cb.create();
         Root<CompUser> c = q.from(CompUser.class);
-        Parameter<String> param = cb.parameter(String.class);
+        Parameter<String> param = cb.parameter(String.class, "name");
         q.where(cb.like(c.get(CompUser_.name), param, '|'));
         q.select(c.get(CompUser_.name));
         
-        assertEquivalence(q, query, new Object[] {"%|_%"});
+        assertEquivalence(q, query, new String[]{"name"}, 
+                new Object[] {"%|_%"});
     }
 
-    public void testNullExprUsingCriteria() {
+    public void testNullExpression() {
         String query =
             "SELECT o.name FROM CompUser o WHERE o.age IS NOT NULL AND " +
             "o.computerName = 'PC' ";
         CriteriaQuery q = cb.create();
         Root<CompUser> c = q.from(CompUser.class);
-        Parameter<String> param = cb.parameter(String.class);
         q.where(cb.and(cb.notEqual(c.get(CompUser_.age), null), 
                 cb.equal(c.get(CompUser_.computerName), "PC")));
         q.select(c.get(CompUser_.name));
@@ -200,7 +185,6 @@
 
         CriteriaQuery q = cb.create();
         Root<CompUser> c = q.from(CompUser.class);
-        Parameter<String> param = cb.parameter(String.class);
         q.where(cb.equal(c.get(CompUser_.address).get(Address_.country),
             null));
         q.select(c.get(CompUser_.name));
@@ -209,7 +193,7 @@
     }
     
     // do not support isEmpty for array fields
-    @AllowFailure
+    
     public void testIsEmptyExprUsingCriteria() {
         String query =
             "SELECT o.name FROM CompUser o WHERE o.nicknames IS NOT EMPTY";
@@ -221,80 +205,11 @@
         assertEquivalence(cq, query);
     }
 
-    @AllowFailure(message="Address is embeddable. Can it be in " +
-    		"subquery/From Clause?")
-    public void testExistExprUsingCriteria() {
-        String query = "SELECT DISTINCT o.name FROM CompUser o WHERE EXISTS" +
-            " (SELECT c FROM Address c WHERE c = o.address )";
-
-        CriteriaQuery q = cb.create();
-        Root<CompUser> o = q.from(CompUser.class);
-        Subquery<Address> sq = q.subquery(Address.class);
-        sq.correlate(o);
-        Root<Address> c = sq.from(Address.class);
-        sq.select(c);
-        sq.where(cb.equal(c, o.get(CompUser_.address)));
-        q.where(cb.exists(sq));
-        q.select(o.get(CompUser_.name)).distinct(true);
-        assertEquivalence(q, query);
-        List result = em.createQuery(q).getResultList();
-
-        assertNotNull("the list is null", result);
-        assertEquals("they are not equal", 5, result.size());
-        assertTrue("Seetha is not list", result.contains("Seetha"));
-        assertTrue("Shannon is not list", result.contains("Shannon"));
-        assertTrue("jacob is not list", result.contains("Jacob"));
-        assertTrue("ugo is not list", result.contains("Ugo"));
-
-        em.clear();
-    }
     
-    @AllowFailure
-    public void testNotExistExprUsingCriteria() {
-        String query =
-            "SELECT DISTINCT o.name FROM CompUser o WHERE NOT EXISTS" +
-                " (SELECT s FROM CompUser s WHERE s.address.country = " +
-                "o.address.country)";
-
-        CriteriaQuery q = cb.create();
-        Root<CompUser> o = q.from(CompUser.class);
-        Subquery<CompUser> sq = q.subquery(CompUser.class);
-        sq.correlate(o);
-        Root<CompUser> s = sq.from(CompUser.class);
-        sq.select(s);
-        sq.where(cb.equal(s.get(CompUser_.address).get(Address_.country), 
-                o.get(CompUser_.address).get(Address_.country)));
-        q.where(cb.exists(sq).negate());
-        q.select(o.get(CompUser_.name)).distinct(true);
-        
-        assertEquivalence(q, query);
-    }
-
-    @AllowFailure
-    public void testAnyExprUsingCriteria() {
-        String query =
-            "SELECT o.name FROM CompUser o WHERE o.address.zipCode = ANY (" +
-                " SELECT s.computerName FROM CompUser s WHERE " +
-                "s.address.country IS NOT NULL )";
-
-        CriteriaQuery q = cb.create();
-        Root<CompUser> o = q.from(CompUser.class);
-        q.select(o.get(CompUser_.name));
-        Subquery<String> sq = q.subquery(String.class);
-        Root<CompUser> s = sq.from(CompUser.class);
-        sq.select(s.get(CompUser_.computerName));
-        sq.where(cb.notEqual(s.get(CompUser_.address).get(Address_.country),
-            null));
-        q.where(cb.equal(o.get(CompUser_.address).get(Address_.zipCode), 
-            cb.any(sq)));
-        
-        assertEquivalence(q, query);
-    }
     
-    @AllowFailure(message="ResultPacker is all confused")
     public void testConstructorExprUsingCriteria() {
         String query =
-            "SELECT NEW org.apache.openjpa.persistence.common.apps.MaleUser(" +
+            "SELECT NEW org.apache.openjpa.persistence.criteria.MaleUser(" +
             "c.name, c.computerName, c.address, c.age, c.userid)" +
             " FROM CompUser c WHERE c.name = 'Seetha'";
         
@@ -497,7 +412,7 @@
         assertEquivalence(q, query);
     }
     
-    @AllowFailure
+    
     public void testArithmFunc3() {
         String query =
             "select MOD(e.age, 4) From CompUser e WHERE e.name='Seetha'";
@@ -510,8 +425,6 @@
         assertEquivalence(q, query);
     }
     
-    // size method can not be applied to an array field
-    @AllowFailure(message="size method can not be applied to an array field?")
     public void testArithmFunc4() {
         String query = "SELECT e.name FROM CompUser e WHERE " +
         		"SIZE(e.nicknames) = 6";
@@ -588,7 +501,6 @@
         assertEquivalence(q, query);
     }
 
-    @AllowFailure(message="criteria does not generate Optimize for 1 row")
     public void testMINAggregFunc() {
         String query = "SELECT DISTINCT MIN(c.age) FROM CompUser c";
 
@@ -611,39 +523,39 @@
         assertEquivalence(q, query);
     }
 
-    // can not do TYPE with parameter in the IN clause
-    @AllowFailure
     public void testTypeExpression1() {
-        String query = "SELECT e FROM CompUser e where TYPE(e) in (?1, ?2) " +
+        String jpql = "SELECT e FROM CompUser e where TYPE(e) in (:a, :b) " +
             "ORDER By e.name";
-        CriteriaQuery q = cb.create();
-        q = cb.create();
-        Root<CompUser> e = q.from(CompUser.class);
-        q.select(e);
-        Parameter<Class> param1 = cb.parameter(Class.class);
-        Parameter<Class> param2 = cb.parameter(Class.class);
-        // q.where(cb.in(e.type()).value(param1).value(param2));
-        q.orderBy(cb.asc(e.get(CompUser_.name)));
         
-        assertEquivalence(q, query);
+        CriteriaQuery cq = cb.create();
+        Root<CompUser> e = cq.from(CompUser.class);
+        cq.select(e);
+        Parameter<Class> param1 = cb.parameter(Class.class, "a");
+        Parameter<Class> param2 = cb.parameter(Class.class, "b");
+        cq.where(e.type().in(param1, param2));
+        cq.orderBy(cb.asc(e.get(CompUser_.name)));
+        
+        assertEquivalence(cq, jpql, new String[]{"a","b"}, 
+             new Class[]{MaleUser.class,FemaleUser.class});
     }
 
-    @AllowFailure
+    
     public void testTypeExpression2() {
-        String query = "SELECT TYPE(e) FROM CompUser e where TYPE(e) <> ?1";
+        String query = "SELECT TYPE(e) FROM CompUser e where TYPE(e) <> :t";
         CriteriaQuery q = cb.create();
         q = cb.create();
         Root<CompUser> e = q.from(CompUser.class);
-        Parameter<Class> param1 = cb.parameter(Class.class);
+        Parameter<Class> param1 = cb.parameter(Class.class, "t");
         q.select(e.type());
         q.where(cb.equal(e.type(), param1).negate());
         
-        assertEquivalence(q, query);
+        assertEquivalence(q, query, new String[]{"t"}, 
+                new Class[]{MaleUser.class});
     }
 
     // Type literal
     // this Cartesian problem can not be rewritten to use JOIN
-    @AllowFailure
+    
     public void testTypeExpression3() {
         String query = "SELECT e, FemaleUser, a FROM Address a, FemaleUser e "
                 + " where e.address IS NOT NULL";
@@ -692,7 +604,7 @@
         assertEquivalence(cq, query);
     }
 
-    @AllowFailure
+    
     public void testTypeExpression7() {
         String query = "SELECT TYPE(a.b) FROM A a";
         CriteriaQuery q = cb.create();
@@ -885,7 +797,7 @@
 
     // not sure how to write CriteriaQuery for
     // Subquery.select(SimpleCase/GeneralCase)
-    @AllowFailure
+    
     public void testGeneralCaseExpression4() {
         String query = "select e.name, e.creditRating from CompUser e "
             + "where e.creditRating = "
@@ -922,529 +834,4 @@
         assertEquivalence(q, query);
     }
 
-    @AllowFailure
-    public void testSubquery1() {
-        String query = "select o1.id from Order o1 where o1.id in "
-                + " (select distinct o.id from LineItem i, Order o"
-                + " where i.quantity > 10 and o.count > 1000 and i.id = o.id)";
-        
-        CriteriaQuery q = cb.create();
-        Root<Order> o1 = q.from(Order.class);
-        q.select(o1.get(Order_.id));
-
-        Subquery<Integer> sq = q.subquery(Integer.class);
-        Root<LineItem> i = sq.from(LineItem.class);
-        Join<LineItem, Order> o = i.join(LineItem_.order);
-        sq.where(cb.and(cb.and(cb.gt(i.get(LineItem_.quantity), 10), cb.gt(o
-                .get(Order_.count), 1000)), cb.equal(i.get(LineItem_.id), o
-                .get(Order_.id))));
-        sq.select(o.get(Order_.id)).distinct(true);
-        q.where(cb.in(o1.get(Order_.id)).value(
-                sq.select(o.get(Order_.id)).distinct(true)));
-        
-        assertEquivalence(q, query);
-    }
-
-    @AllowFailure
-    public void testSubquery2() {
-        String query = "select o.id from Order o where o.customer.balanceOwed ="
-                + " (select max(o2.customer.balanceOwed) from Order o2"
-                + " where o.customer.id = o2.customer.id)";
-        CriteriaQuery q = cb.create();
-        Root<Order> o = q.from(Order.class);
-        q.select(o.get(Order_.id));
-        Subquery<Integer> sq = q.subquery(Integer.class);
-        Root<Order> o2 = sq.from(Order.class);
-        sq.where(cb.equal(o.get(Order_.customer).get(Customer_.id), o2.get(
-                Order_.customer).get(Customer_.id)));
-        q.where(cb.equal(o.get(Order_.customer).get(Customer_.balanceOwed), sq
-                .select(cb.max(o2.get(Order_.customer).get(
-                        Customer_.balanceOwed)))));
-        
-        assertEquivalence(q, query);
-    }
-
-    @AllowFailure
-    public void testSubquery3() {
-        String query = "select o from Order o where o.customer.balanceOwed ="
-                + " (select max(o2.customer.balanceOwed) from Order o2"
-                + " where o.customer.id = o2.customer.id)";
-        CriteriaQuery q = cb.create();
-        Root<Order> o = q.from(Order.class);
-        q.select(o);
-        Subquery<Integer> sq = q.subquery(Integer.class);
-        Root<Order> o2 = sq.from(Order.class);
-        sq.where(cb.equal(o.get(Order_.customer).get(Customer_.id), o2.get(
-                Order_.customer).get(Customer_.id)));
-        q.where(cb.equal(o.get(Order_.customer).get(Customer_.balanceOwed), sq
-                .select(cb.max(o2.get(Order_.customer).get(
-                        Customer_.balanceOwed)))));
-        
-        assertEquivalence(q, query);
-    }
-
-    @AllowFailure
-    public void testSubquery4() {
-        String query = "select o.id from Order o where o.quantity >"
-                + " (select count(i) from o.lineItems i)";
-        CriteriaQuery q = cb.create();
-        Root<Order> o = q.from(Order.class);
-        q.select(o.get(Order_.id));
-        Subquery<Long> sq = q.subquery(Long.class);
-        Root<Order> osq = sq.correlate(o);
-        Join<Order, LineItem> i = osq.join(Order_.lineItems);
-        q.where(cb.gt(o.get(Order_.quantity), sq.select(cb.count(i))));
-        assertEquivalence(q, query);
-    }
-
-    @AllowFailure
-    public void testSubquery5() {
-        String query = "select o.id from Order o where o.quantity >"
-                + " (select count(o.quantity) from Order o)";
-        CriteriaQuery q = cb.create();
-        Root<Order> o = q.from(Order.class);
-        q.select(o.get(Order_.id));
-        Subquery<Long> sq = q.subquery(Long.class);
-        Root<Order> o2 = sq.from(Order.class);
-        q.where(cb.gt(o.get(Order_.quantity), sq.select(cb.count(o2
-                .get(Order_.quantity)))));
-        
-        assertEquivalence(q, query);
-    }
-
-    @AllowFailure
-    public void testSubquery6() {
-        String query = "select o.id from Order o where o.quantity >"
-                + " (select count(o.id) from Order o)";
-        CriteriaQuery q = cb.create();
-        Root<Order> o = q.from(Order.class);
-        q.select(o.get(Order_.id));
-        Subquery<Long> sq = q.subquery(Long.class);
-        Root<Order> o2 = sq.from(Order.class);
-        q.where(cb.gt(o.get(Order_.quantity), sq.select(cb.count(o2
-                .get(Order_.id)))));
-        
-        assertEquivalence(q, query);
-    }
-
-    @AllowFailure
-    public void testSubquery7() {
-        String query = "select o.id from Order o where o.quantity >"
-                + " (select avg(o.quantity) from Order o)";
-        CriteriaQuery q = cb.create();
-        Root<Order> o = q.from(Order.class);
-        q.select(o.get(Order_.id));
-        Subquery<Double> sq = q.subquery(Double.class);
-        Root<Order> o2 = sq.from(Order.class);
-        q.where(cb.gt(o.get(Order_.quantity), sq.select(cb.avg(o2
-                .get(Order_.quantity)))));
-        
-        assertEquivalence(q, query);
-    }
-
-    @AllowFailure
-    public void testSubquery8() {
-        String query = "select c.name from Customer c where exists"
-                + " (select o from c.orders o where o.id = 1) or exists"
-                + " (select o from c.orders o where o.id = 2)";
-        CriteriaQuery q = cb.create();
-        Root<Customer> c = q.from(Customer.class);
-        q.select(c.get(Customer_.name));
-        Subquery<Order> sq1 = q.subquery(Order.class);
-        Root<Customer> c1 = sq1.correlate(c);
-        SetJoin<Customer, Order> o1 = c1.join(Customer_.orders);
-        sq1.where(cb.equal(o1.get(Order_.id), 1)).select(o1);
-
-        Subquery<Order> sq2 = q.subquery(Order.class);
-        Root<Customer> c2 = sq2.correlate(c);
-        SetJoin<Customer, Order> o2 = c2.join(Customer_.orders);
-        sq2.where(cb.equal(o2.get(Order_.id), 2)).select(o2);
-
-        q.where(cb.or(cb.exists(sq1), cb.exists(sq2)));
-        
-        assertEquivalence(q, query);
-    }
-
-    @AllowFailure
-    public void testSubquery9() {
-        String query = "select c.name from Customer c, in(c.orders) o "
-                + "where o.quantity between "
-                + "(select max(o.quantity) from Order o) and "
-                + "(select avg(o.quantity) from Order o) ";
-        CriteriaQuery q = cb.create();
-        Root<Customer> c = q.from(Customer.class);
-        q.select(c.get(Customer_.name));
-
-        Subquery<Integer> sq1 = q.subquery(Integer.class);
-        Root<Order> o1 = sq1.from(Order.class);
-        sq1.select(cb.max(o1.get(Order_.quantity)));
-
-        Subquery<Double> sq2 = q.subquery(Double.class);
-        Root<Order> o2 = sq2.from(Order.class);
-        sq2.select(cb.avg(o2.get(Order_.quantity)));
-
-        SetJoin<Customer, Order> o = c.join(Customer_.orders);
-        // not sure how to do call between of integer(quantity)
-        // between integer (max quantity) and double (avg quantity)
-        // q.where(cb.between(o.get(Order_.quantity), sq1, sq2));
-        
-        assertEquivalence(q, query);
-    }
-
-    @AllowFailure
-    public void testSubquery10() {
-        String query = "select o.id from Order o where o.quantity >"
-                + " (select sum(o2.quantity) from Customer c, " 
-                + "in(c.orders) o2) ";
-        CriteriaQuery q = cb.create();
-        Root<Order> o = q.from(Order.class);
-        q.select(o.get(Order_.id));
-
-        Subquery<Integer> sq = q.subquery(Integer.class);
-        Root<Customer> c = sq.from(Customer.class);
-        SetJoin<Customer, Order> o2 = c.join(Customer_.orders);
-        sq.select(cb.sum(o2.get(Order_.quantity)));
-
-        q.where(cb.gt(o2.get(Order_.quantity), sq));
-        
-        assertEquivalence(q, query);
-    }
-
-    @AllowFailure
-    public void testSubquery11() {
-        String query = "select o.id from Order o where o.quantity between"
-                + " (select avg(o2.quantity) from Customer c, in(c.orders) o2)"
-                + " and (select min(o2.quantity) from Customer c, in(c.orders)"
-                + " o2)";
-        CriteriaQuery q = cb.create();
-        Root<Order> o = q.from(Order.class);
-        q.select(o.get(Order_.id));
-
-        Subquery<Double> sq1 = q.subquery(Double.class);
-        Root<Customer> c = sq1.from(Customer.class);
-        SetJoin<Customer, Order> o2 = c.join(Customer_.orders);
-        sq1.select(cb.avg(o2.get(Order_.quantity)));
-
-        Subquery<Integer> sq2 = q.subquery(Integer.class);
-        Root<Customer> c2 = sq2.from(Customer.class);
-        SetJoin<Customer, Order> o3 = c2.join(Customer_.orders);
-        sq2.select(cb.min(o3.get(Order_.quantity)));
-
-        // do not know how to call between for double and integer
-        // q.where(cb.between(o2.get(Order_.quantity), sq1, sq2));
-        assertEquivalence(q, query);
-    }
-
-    @AllowFailure
-    public void testSubquery12() {
-        String query =
-            "select o.id from Customer c, in(c.orders)o "
-                + "where o.quantity > (select sum(o2.quantity)"
-                + " from c.orders o2)";
-        CriteriaQuery q = cb.create();
-        Root<Customer> c = q.from(Customer.class);
-        SetJoin<Customer, Order> o = c.join(Customer_.orders);
-        q.select(o.get(Order_.id));
-
-        Subquery<Integer> sq = q.subquery(Integer.class);
-        Root<Customer> sqc = sq.correlate(c);
-        SetJoin<Customer, Order> o2 = sqc.join(Customer_.orders);
-        sq.select(cb.sum(o2.get(Order_.quantity)));
-        q.where(cb.gt(o.get(Order_.quantity), sq));
-        
-        assertEquivalence(q, query);
-    }
-
-    @AllowFailure
-    public void testSubquery13() {
-        String query = "select o1.id, c.name from Order o1, Customer c"
-                + " where o1.quantity = "
-                + " any(select o2.quantity from in(c.orders) o2)";
-        CriteriaQuery q = cb.create();
-        Root<Order> o1 = q.from(Order.class);
-        Join<Order, Customer> c = o1.join(Order_.customer);
-        q.select(o1.get(Order_.id), c.get(Customer_.name));
-
-        Subquery<Integer> sq = q.subquery(Integer.class);
-        Join<Order, Customer> sqc = sq.correlate(c);
-        SetJoin<Customer, Order> o2 = sqc.join(Customer_.orders);
-        sq.select(o2.get(Order_.quantity));
-
-        q.where(cb.equal(o1.get(Order_.quantity), cb.any(sq)));
-        
-        assertEquivalence(q, query);
-    }
-
-    @AllowFailure
-    public void testSubquery14() {
-        String query = "SELECT p, m FROM Publisher p "
-            + "LEFT OUTER JOIN p.magazineCollection m "
-            + "WHERE m.id = (SELECT MAX(m2.id) FROM Magazine m2 "
-            + "WHERE m2.idPublisher.id = p.id AND m2.datePublished = "
-            //+ "(SELECT MAX(m3.datePublished) FROM Magazine m3 "
-            + "(SELECT MAX(m3.id) FROM Magazine m3 "
-            + "WHERE m3.idPublisher.id = p.id)) ";
-        CriteriaQuery q = cb.create();
-        Root<Publisher> p = q.from(Publisher.class);
-        Join<Publisher, Magazine> m = p.join(Publisher_.magazineCollection,
-            JoinType.LEFT);
-        q.select(p, m);
-
-        Subquery<Integer> sq = q.subquery(Integer.class);
-        Root<Magazine> m2 = sq.from(Magazine.class);
-        q.where(cb.equal(m.get(Magazine_.id), sq.select(cb.max(m2
-            .get(Magazine_.id)))));
-
-        Subquery<Integer> sq2 = q.subquery(Integer.class);
-        Root<Magazine> m3 = sq2.from(Magazine.class);
-        
-        sq2.where(cb.equal(m3.get(Magazine_.idPublisher).get(Publisher_.id), 
-            p.get(Publisher_.id)));
-        
-        sq.where(cb.and(cb.equal(m2.get(Magazine_.idPublisher).get(
-            Publisher_.id), p.get(Publisher_.id)), cb.equal(m2
-            .get(Magazine_.datePublished), sq2.select(cb.max(m3
-            .get(Magazine_.id))))));
-        
-        assertEquivalence(q, query);
-    }
-
-    // outstanding problem subqueries:
-    // "select o from Order o where o.amount > (select count(o) from Order o)",
-    // "select o from Order o where o.amount > (select count(o2) from Order o2)
-    //",
-    // "select c from Customer c left join c.orders p where not exists"
-    // + " (select o2 from c.orders o2 where o2 = o",
-
-    // not sure how to write CriteriaQuery for
-    // Subquery.select(SimpleCase/GeneralCase)
-    @AllowFailure
-    public void testSubquery15() {
-        String query = "select o.id from Order o where o.delivered =(select "
-                + "   CASE WHEN o2.quantity > 10 THEN true"
-                + "     WHEN o2.quantity = 10 THEN false "
-                + "     ELSE false END from Order o2"
-                + " where o.customer.id = o2.customer.id)";
-        CriteriaQuery q = cb.create();
-        Root<Order> o = q.from(Order.class);
-        q.select(o.get(Order_.id));
-
-        Subquery<Boolean> sq = q.subquery(Boolean.class);
-        Root<Order> o2 = sq.from(Order.class);
-        sq.where(cb.equal(o.get(Order_.customer).get(Customer_.id), o2.get(
-                Order_.customer).get(Customer_.id)));
-
-        q.where(cb.equal(o.get(Order_.delivered),
-        // sq.select(
-                cb.selectCase().when(cb.gt(o2.get(Order_.quantity), 10), true)
-                        .when(cb.equal(o2.get(Order_.quantity), 10), false)
-                        .otherwise(false)
-        // )
-                ));
-        
-        assertEquivalence(q, query);
-    }
-
-    @AllowFailure
-    public void testSubquery16() {
-        String query = "select o1.id from Order o1 where o1.quantity > "
-                + " (select o.quantity*2 from LineItem i, Order o"
-                + " where i.quantity > 10 and o.quantity > 1000 and i.id = " +
-                		"o.id)";
-        CriteriaQuery q = cb.create();
-        Root<Order> o1 = q.from(Order.class);
-        q.select(o1.get(Order_.id));
-
-        Subquery<Integer> sq = q.subquery(Integer.class);
-        Root<LineItem> i = sq.from(LineItem.class);
-        Join<LineItem, Order> o = i.join(LineItem_.order);
-        sq.where(cb.and(cb.and(cb.gt(i.get(LineItem_.quantity), 10), cb.gt(o
-                .get(Order_.quantity), 1000)), cb.equal(i.get(LineItem_.id), o
-                .get(Order_.id))));
-
-        q.where(cb.gt(o1.get(Order_.quantity), sq.select(cb.prod(o
-                .get(Order_.quantity), 2))));
-
-        assertEquivalence(q, query);
-    }
-
-    @AllowFailure
-    public void testSubquery17() {
-        String query = "select o.id from Order o where o.customer.name ="
-                + " (select substring(o2.customer.name, 3) from Order o2"
-                + " where o.customer.id = o2.customer.id)";
-        CriteriaQuery q = cb.create();
-        Root<Order> o = q.from(Order.class);
-        q.select(o.get(Order_.customer).get(Customer_.name));
-
-        Subquery<String> sq = q.subquery(String.class);
-        Root<Order> o2 = sq.from(Order.class);
-        sq.where(cb.equal(o.get(Order_.customer).get(Customer_.id), o2.get(
-                Order_.customer).get(Customer_.id)));
-
-        q.where(cb.equal(o.get(Order_.customer).get(Customer_.name), sq
-                .select(cb.substring(o2.get(Order_.customer)
-                        .get(Customer_.name), 3))));
-
-        assertEquivalence(q, query);
-    }
-
-    @AllowFailure
-    public void testSubquery18() {
-        String query = "select o.id from Order o where o.orderTs >"
-                + " (select CURRENT_TIMESTAMP from o.lineItems i)";
-        CriteriaQuery q = cb.create();
-        Root<Order> o = q.from(Order.class);
-        q.select(o.get(Order_.id));
-
-        Subquery<Timestamp> sq = q.subquery(Timestamp.class);
-        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())));
-        
-        assertEquivalence(q, query);
-    }
-
-    @AllowFailure
-    public void testSubquery19() {
-        String query = "select o.id from Order o where o.quantity >"
-                + " (select SQRT(o.quantity) from Order o where o.delivered" +
-                		" = true)";
-        CriteriaQuery q = cb.create();
-        Root<Order> o = q.from(Order.class);
-        q.select(o.get(Order_.id));
-
-        Subquery<Double> sq = q.subquery(Double.class);
-        Root<Order> o2 = sq.from(Order.class);
-        sq.where(cb.equal(o2.get(Order_.delivered), true));
-
-        q.where(cb.gt(o.get(Order_.quantity), sq.select(cb.sqrt(o2
-                .get(Order_.quantity)))));
-        assertEquivalence(q, query);
-    }
-
-    @AllowFailure
-    public void testSubquery20() {
-        String query = "select o.id from Order o where o.customer.name in"
-                + " (select CONCAT(o.customer.name, 'XX') from Order o"
-                + " where o.quantity > 10)";
-        CriteriaQuery q = cb.create();
-        Root<Order> o = q.from(Order.class);
-        q.select(o.get(Order_.id));
-
-        Subquery<String> sq = q.subquery(String.class);
-        Root<Order> o2 = sq.from(Order.class);
-        sq.where(cb.gt(o2.get(Order_.quantity), 10));
-
-        q.where(cb.in(o.get(Order_.customer).get(Customer_.name)).value(
-                sq.select(cb.concat(
-                        o2.get(Order_.customer).get(Customer_.name), "XX"))));
-        assertEquivalence(q, query);
-    }
-
-    @AllowFailure
-    public void testSubquery21() {
-        String query = "select c from Customer c where c.creditRating ="
-                + " (select "
-                + "   CASE WHEN o2.quantity > 10 THEN "
-                + "org.apache.openjpa.persistence.criteria.Customer$" +
-                		"CreditRating.POOR"
-                + "     WHEN o2.quantity = 10 THEN "
-                + "org.apache.openjpa.persistence.criteria.Customer$" +
-                		"CreditRating.GOOD "
-                + "     ELSE "
-                + "org.apache.openjpa.persistence.criteria.Customer$" +
-                		"CreditRating.EXCELLENT "
-                + "     END from Order o2"
-                + " where c.id = o2.customer.id)";
-        CriteriaQuery q = cb.create();
-        Root<Customer> c = q.from(Customer.class);
-        q.select(c);
-
-        Subquery<String> sq = q.subquery(String.class);
-        Root<Order> o2 = sq.from(Order.class);
-        sq.where(cb.equal(c.get(Customer_.id), o2.get(Order_.customer).get(
-            Customer_.id)));
-
-        q.where(cb.equal(c.get(Customer_.creditRating), 
-            //sq.select(
-            cb.selectCase().when(cb.gt(o2.get(Order_.quantity), 10),
-                Customer.CreditRating.POOR).when(
-                cb.equal(o2.get(Order_.quantity), 10),
-                Customer.CreditRating.GOOD).otherwise(
-                Customer.CreditRating.EXCELLENT)
-        // )
-            ));
-        assertEquivalence(q, query);
-    }
-
-    // Coalesce for Enum type
-    @AllowFailure
-    public void testSubquery22() {
-        String query = "select c from Customer c "
-                + "where c.creditRating = (select COALESCE (c1.creditRating, "
-                + "org.apache.openjpa.persistence.criteria.Customer$" +
-                		"CreditRating.POOR) "
-                + "from Customer c1 where c1.name = 'Famzy') order by c.name " +
-                		"DESC";
-        CriteriaQuery q = cb.create();
-        Root<Customer> c = q.from(Customer.class);
-        q.select(c);
-        q.orderBy(cb.desc(c.get(Customer_.name)));        
-
-        Subquery<Customer.CreditRating> sq =
-            q.subquery(Customer.CreditRating.class);
-        Root<Customer> c1 = sq.from(Customer.class);
-        sq.where(cb.equal(c1.get(Customer_.name), "Famzy"));
-        
-        //q.where(cb.equal(c.get(Customer_.creditRating),
-        //    sq.select(cb.coalesce().value(c1.get(Customer_.creditRating).
-        //        value(Customer.CreditRating.POOR)))));    
-        assertEquivalence(q, query);
-    }
-
-    @AllowFailure
-    public void testSubquery23() {
-        String query =
-            "select c from Customer c "
-                + "where c.creditRating = (select NULLIF (c1.creditRating, "
-                + "org.apache.openjpa.persistence.criteria."
-                + "Customer$CreditRating.POOR) "
-                + "from Customer c1 where c1.name = 'Famzy') "
-                + "order by c.name DESC";
-        CriteriaQuery q = cb.create();
-        Root<Customer> c = q.from(Customer.class);
-        q.select(c);
-        q.orderBy(cb.desc(c.get(Customer_.name)));        
-
-        Subquery<Customer.CreditRating> sq =
-            q.subquery(Customer.CreditRating.class);
-        Root<Customer> c1 = sq.from(Customer.class);
-        sq.where(cb.equal(c1.get(Customer_.name), "Famzy"));
-        
-        q.where(cb.equal(c.get(Customer_.creditRating),
-            sq.select(cb.nullif(c1.get(Customer_.creditRating),
-                Customer.CreditRating.POOR))));    
-        assertEquivalence(q, query);
-    }
-
-    /**
-     * Verify a sub query can contain MAX and additional date comparisons
-     * without losing the correct alias information. This sort of query
-     * originally caused problems for DBDictionaries which used DATABASE syntax.
-     */
-    // Not sure how to do Cartesian join when Employee can not 
-    // navigate to Dependent
-    @AllowFailure
-    public void testSubSelectMaxDateRange() {
-        String query = "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 ";
-    }
-
 }

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=785341&r1=785340&r2=785341&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 Tue Jun 16 18:56:57 2009
@@ -29,6 +29,7 @@
 import javax.persistence.criteria.ListJoin;
 import javax.persistence.criteria.MapJoin;
 import javax.persistence.criteria.Root;
+import javax.persistence.criteria.SetJoin;
 import javax.persistence.criteria.Subquery;
 
 import org.apache.openjpa.persistence.test.AllowFailure;
@@ -193,7 +194,6 @@
         assertEquivalence(cq, jpql);
     }
     
-    @AllowFailure(message = "Extra joins created in Criteria")
     public void testMultiLevelJoins() {
         String jpql = "SELECT c FROM Customer c JOIN c.orders o "
             + "JOIN o.lineItems i WHERE i.product.productType = 'printer'";
@@ -221,7 +221,7 @@
         assertEquivalence(c, jpql);
     }
 
-    @AllowFailure(message = "Fetch joins not implemented")
+    @AllowFailure(message="FetchJoin not implemented")
     public void testFetchJoins() {
         String jpql = "SELECT d FROM Department LEFT JOIN FETCH d.employees "
             + "WHERE d.deptNo = 1";
@@ -233,12 +233,15 @@
         assertEquivalence(q, jpql);
     }
 
-    @AllowFailure(message = "Invalid SQL is generated")
     public void testJoinedPathInProjection() {
-        String jpql = "SELECT p.vendor FROM Employee e JOIN "
+        String jpql1 = "SELECT p.vendor FROM Employee e JOIN "
             + "e.contactInfo.phones p "
             + "WHERE e.contactInfo.address.zipCode = '95054'";
-        
+
+        String jpql = "SELECT p.vendor FROM Employee e JOIN "
+            + "e.contactInfo c JOIN c.phones p "
+            + "WHERE c.address.zipCode = '95054'";
+
         CriteriaQuery cq = cb.create();
         Root<Employee> e = cq.from(Employee.class);
         Join<Contact, Phone> p = e.join(Employee_.contactInfo).join(
@@ -250,11 +253,14 @@
         assertEquivalence(cq, jpql);
     }
 
-    @AllowFailure(message = "Key expression not implemented")
     public void testKeyExpression() {
-        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%'";
+        String jpql = "select i.name, VALUE(p)"
+            + " from Item i join i.photos p"
+            + " where KEY(p) like 'egret'";
+
         CriteriaQuery q = cb.create();
         Root<Item> item = q.from(Item.class);
         MapJoin<Item, String, Photo> photo = item.join(Item_.photos);
@@ -264,7 +270,6 @@
         assertEquivalence(q, jpql);
     }
 
-    @AllowFailure(message = "Index expression not implemented")
     public void testIndexExpression() {
         String jpql = "SELECT t FROM CreditCard c JOIN c.transactionHistory t "
             + "WHERE c.customer.accountNum = 321987 AND INDEX(t) BETWEEN 0 "
@@ -273,9 +278,10 @@
         Root<CreditCard> c = q.from(CreditCard.class);
         ListJoin<CreditCard, TransactionHistory> t = c
         .join(CreditCard_.transactionHistory);
-        q.select(t).where(
-                cb.equal(c.get(CreditCard_.customer).get(Customer_.accountNum),
-                        321987), cb.between(t.index(), 0, 9));
+        q.select(t).where(cb.and(
+            cb.equal(c.get(CreditCard_.customer).get(Customer_.accountNum),
+                321987), 
+            cb.between(t.index(), 0, 9)));
 
         assertEquivalence(q, jpql);
     }
@@ -289,9 +295,8 @@
         assertEquivalence(q, jpql);
     }
 
-    @AllowFailure(message = "broken")
     public void testExpressionInProjection() {
-        String jpql = "SELECT o.quantity, o.totalCost*1.08 AS taxedCost, "
+        String jpql = "SELECT o.quantity, o.totalCost*1.08, "
             + "a.zipCode FROM Customer c JOIN c.orders o JOIN c.address a "
             + "WHERE a.state = 'CA' AND a.county = 'Santa Clara'";
         
@@ -299,8 +304,9 @@
         Root<Customer> c = cq.from(Customer.class);
         Join<Customer, Order> o = c.join(Customer_.orders);
         Join<Customer, Address> a = c.join(Customer_.address);
-        cq.where(cb.equal(a.get(Address_.state), "CA"), 
-                 cb.equal(a.get(Address_.county), "Santa Clara"));
+        cq.where(cb.and(
+            cb.equal(a.get(Address_.state), "CA"), 
+            cb.equal(a.get(Address_.county), "Santa Clara")));
         cq.select(o.get(Order_.quantity), cb.prod(o
                 .get(Order_.totalCost), 1.08), a.get(Address_.zipCode));
 
@@ -316,20 +322,19 @@
         assertEquivalence(q, jpql);
     }
 
-    @AllowFailure(message = "Index expression not implemented")
     public void testIndexExpressionAndLietral() {
         String jpql = "SELECT w.name FROM Course c JOIN c.studentWaitList w "
             + "WHERE c.name = 'Calculus' AND INDEX(w) = 0";
         CriteriaQuery q = cb.create();
         Root<Course> course = q.from(Course.class);
         ListJoin<Course, Student> w = course.join(Course_.studentWaitList);
-        q.where(cb.equal(course.get(Course_.name), "Calculus"),
-                cb.equal(w.index(), 0)).select(w.get(Student_.name));
+        q.where(cb.and(
+            cb.equal(course.get(Course_.name), "Calculus"),
+            cb.equal(w.index(), 0))).select(w.get(Student_.name));
 
         assertEquivalence(q, jpql);
     }
 
-    @AllowFailure(message = "SQL for Criteria has extra join")
     public void testAggregateInProjection() {
         String jpql = "SELECT SUM(i.price) FROM Order o JOIN o.lineItems i " + 
             "JOIN o.customer c WHERE c.lastName = 'Smith' AND " + 
@@ -338,8 +343,9 @@
         Root<Order> o = q.from(Order.class);
         Join<Order, LineItem> i = o.join(Order_.lineItems);
         Join<Order, Customer> c = o.join(Order_.customer);
-        q.where(cb.equal(c.get(Customer_.lastName), "Smith"), cb.equal(c
-                .get(Customer_.firstName), "John"));
+        q.where(cb.and(
+            cb.equal(c.get(Customer_.lastName), "Smith"), 
+            cb.equal(c.get(Customer_.firstName), "John")));
         q.select(cb.sum(i.get(LineItem_.price)));
 
         assertEquivalence(q, jpql);
@@ -376,48 +382,32 @@
         assertEquivalence(q, jpql);
     }
 
-    @AllowFailure(message = "Extra Joins created")
     public void testExpression1() {
-        String jpql = "SELECT o.quantity, o.totalCost*1.08 AS taxedCost, "
+        String jpql = "SELECT o.quantity, o.totalCost*1.08, "
             + "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.create();
         Root<Customer> cust = q.from(Customer.class);
         Join<Customer, Order> order = cust.join(Customer_.orders);
         Join<Customer, Address> address = cust.join(Customer_.address);
-        q.where(cb.equal(address.get(Address_.state), "CA"), cb.equal(address
-                .get(Address_.county), "Santa Clara"));
+        q.where(cb.and(
+            cb.equal(address.get(Address_.state), "CA"), 
+            cb.equal(address.get(Address_.county), "Santa Clara")));
         q.select(order.get(Order_.quantity), cb.prod(order
                 .get(Order_.totalCost), 1.08), address.get(Address_.zipCode));
 
         assertEquivalence(q, jpql);
     }
 
-    @AllowFailure(message = "Index expression not implemented")
     public void testExpression3() {
         String jpql = "SELECT w.name FROM Course c JOIN c.studentWaitList w "
             + "WHERE c.name = 'Calculus' AND INDEX(w) = 0";
         CriteriaQuery q = cb.create();
         Root<Course> course = q.from(Course.class);
         ListJoin<Course, Student> w = course.join(Course_.studentWaitList);
-        q.where(cb.equal(course.get(Course_.name), "Calculus"),
-                cb.equal(w.index(), 0)).select(w.get(Student_.name));
-
-        assertEquivalence(q, jpql);
-    }
-
-    @AllowFailure(message = "Generates extra Join")
-    public void testExpression4() {
-        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.create();
-        Root<Order> o = q.from(Order.class);
-        Join<Order, LineItem> i = o.join(Order_.lineItems);
-        Join<Order, Customer> c = o.join(Order_.customer);
-        q.where(cb.equal(c.get(Customer_.lastName), "Smith"), cb.equal(c
-                .get(Customer_.firstName), "John"));
-        q.select(cb.sum(i.get(LineItem_.price)));
+        q.where(cb.and(
+            cb.equal(course.get(Course_.name), "Calculus"),
+            cb.equal(w.index(), 0))).select(w.get(Student_.name));
 
         assertEquivalence(q, jpql);
     }
@@ -509,35 +499,34 @@
     }
 
     public void testParameters3() {
-        String jpql = "SELECT c FROM Customer c Where c.status = ?1";
+        String jpql = "SELECT c FROM Customer c Where c.status = :stat";
         CriteriaQuery q = cb.create();
         Root<Customer> c = q.from(Customer.class);
-        Parameter<Integer> param = cb.parameter(Integer.class);
+        Parameter<Integer> param = cb.parameter(Integer.class, "stat");
         q.select(c).where(cb.equal(c.get(Customer_.status), param));
-        assertEquivalence(q, jpql, new Object[] { 1 });
+        assertEquivalence(q, jpql, new String[]{"stat"}, new Object[] { 1 });
     }
 
-    @AllowFailure(message="add QuotedNumbersInQueries=true otherwise " + 
-    "AbstractExpressionBuilder.convertTypes() compliants")
     public void testParameters4() {
-        String jpql = "SELECT c FROM Customer c Where c.status = ?1 AND "
-            + "c.name = ?2";
+        String jpql = "SELECT c FROM Customer c Where c.status = :stat AND "
+            + "c.name = :name";
         CriteriaQuery q = cb.create();
         Root<Customer> c = q.from(Customer.class);
-        Parameter<Integer> param1 = cb.parameter(Integer.class);
-        Parameter<Integer> param2 = cb.parameter(Integer.class);
-        q.select(c).where(
-                cb.and(cb.equal(c.get(Customer_.status), param1), cb.equal(c
-                        .get(Customer_.name), param2)));
-        assertEquivalence(q, jpql, new Object[] { 1, "test" });
+        Parameter<Integer> param1 = cb.parameter(Integer.class, "stat");
+        Parameter<String> param2 = cb.parameter(String.class, "name");
+        q.select(c).where(cb.and(cb.equal(c.get(Customer_.status), param1), 
+                cb.equal(c.get(Customer_.name), param2)));
+        assertEquivalence(q, jpql, new String[]{"stat", "name"},
+                new Object[] { 1, "test" });
     }
     
-    @AllowFailure(message = "Criteria API does not allow collection parameter")
+    @AllowFailure(message="collection valued parameter does not work in in()")
     public void testParameters5() {
         String jpql = "SELECT c FROM Customer c Where c.status IN (:coll)";
         CriteriaQuery q = cb.create();
         Root<Customer> c = q.from(Customer.class);
-        Parameter<List> param1 = cb.parameter(List.class);
+        Parameter<List> param1 = cb.parameter(List.class, "coll");
+        q.where(c.get(Customer_.status).in(param1));
         // q.select(c).where(cb.in(c.get(Customer_.status)).value(params1));
         List vals = new ArrayList();
         vals.add(1);
@@ -546,7 +535,7 @@
             new Object[] {vals});
     }
     
-    @AllowFailure(message="Value() expression not implemented")
+    @AllowFailure(message="Generates invalid SQL")
     public void testSelectList1() {
         String jpql = "SELECT v.location.street, KEY(i).title, VALUE(i) FROM "
             + "VideoStore v JOIN v.videoInventory i WHERE v.location.zipCode = "
@@ -555,8 +544,9 @@
         Root<VideoStore> v = q.from(VideoStore.class);
         MapJoin<VideoStore, Movie, Integer> inv = v
         .join(VideoStore_.videoInventory);
-        q.where(cb.equal(v.get(VideoStore_.location).get(Address_.zipCode),
-        "94301"), cb.gt(inv.value(), 0));
+        q.where(cb.and(
+        cb.equal(v.get(VideoStore_.location).get(Address_.zipCode),
+        "94301"), cb.gt(inv.value(), 0)));
         q.select(v.get(VideoStore_.location).get(Address_.street), inv.key()
                 .get(Movie_.title), inv.value());
 
@@ -576,37 +566,39 @@
         assertEquivalence(q, jpql);
     }
     
-    @AllowFailure(message="new() in projection with join is broken")
-    public void testNewConstruct1() {
-        String jpql =
-            "SELECT NEW CustomerDetails(c.id, c.status, o.quantity) FROM "
-            + "Customer c JOIN c.orders o WHERE o.quantity > 100";
+    public void testConstructorInProjection() {
+        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.create();
         Root<Customer> c = q.from(Customer.class);
-        Join<Customer, Order> o = c.join(Customer_.orders);
+        SetJoin<Customer, Order> o = c.join(Customer_.orders);
         q.where(cb.gt(o.get(Order_.quantity), 100));
-        q.select(cb.select(CustomerDetails.class, c.get(Customer_.id), c
-                .get(Customer_.status), o.get(Order_.quantity)));
+        q.select(cb.select(CustomerDetails.class, 
+                            c.get(Customer_.id), 
+                            c.get(Customer_.status), 
+                            o.get(Order_.quantity)));
 
         assertEquivalence(q, jpql);
     }
 
-    @AllowFailure(message="JPQL does not support multiple constructors")
-    public void testNewConstruct2() {
-        String jpql = "SELECT NEW CustomerDetails(c.id, c.status), " +
-                "NEW CustomerFullName(c.firstName, c.lastName) FROM "
-            + "Customer c";
+    @AllowFailure(message="Projections of multiple NEW does not work")
+    public void testMultipleConstructorInProjection() {
+        String jpql = "SELECT NEW CustomerDetails(c.id, c.status), " 
+                    + "NEW CustomerFullName(c.firstName, c.lastName) "
+                    + "FROM Customer c";
         
         CriteriaQuery q = cb.create();
         Root<Customer> c = q.from(Customer.class);
-        q.select(cb.select(CustomerDetails.class, c.get(Customer_.id), 
-                c.get(Customer_.status)),
-                cb.select(CustomerFullName.class, c.get(Customer_.firstName), 
-                c.get(Customer_.lastName))
+        q.select(cb.select(CustomerDetails.class, 
+                             c.get(Customer_.id), 
+                             c.get(Customer_.status)),
+                cb.select(CustomerFullName.class, 
+                             c.get(Customer_.firstName), 
+                             c.get(Customer_.lastName))
         );
         assertEquivalence(q, jpql);
     }
-
     
     
     public void testSubqueries1() {
@@ -625,7 +617,6 @@
         assertEquivalence(q, jpql);
     }
 
-    @AllowFailure(message="SQL is incorrect for JPQL, correct for Criteria")
     public void testSubqueries2() {
         String jpql = "SELECT DISTINCT emp FROM Employee emp WHERE EXISTS ("
             + "SELECT spouseEmp FROM Employee spouseEmp WHERE spouseEmp ="
@@ -659,8 +650,6 @@
         assertEquivalence(q, jpql);
     }
 
-    @AllowFailure(message="SQL is incorrect for JPQL, correct for " + 
-        "Criteria is correct with redundant outer joins")
     public void testSubqueries4() {
         String jpql = "SELECT c FROM Customer c WHERE "
             + "(SELECT COUNT(o) FROM c.orders o) > 10";
@@ -675,7 +664,6 @@
         assertEquivalence(q, jpql);
     }
 
-    @AllowFailure(message="Subqueries not implemented")
     public void testSubqueries5() {
         String jpql = "SELECT o FROM Order o WHERE 10000 < ALL ("
             + "SELECT a.balance FROM o.customer c JOIN c.accounts a)";
@@ -692,7 +680,7 @@
         assertEquivalence(q, jpql);
     }
 
-    @AllowFailure(message="Subqueries not implemented")
+    @AllowFailure(message="Root of subquery.delgate is not set")
     public void testSubqueries6() {
         String jpql = "SELECT o FROM Order o JOIN o.customer c WHERE 10000 < "
             + "ALL (SELECT a.balance FROM c.accounts a)";
@@ -722,8 +710,6 @@
         assertEquivalence(q, jpql);
     }
 
-    @AllowFailure(message="AbstractExpressionExecutor.assertNotContainer()"+
-    "not happy")
     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, "
@@ -740,7 +726,6 @@
         assertEquivalence(q, jpql);
     }
 
-    @AllowFailure
     public void testOrdering2() {
         String jpql = "SELECT o.quantity, a.zipCode FROM Customer c "
             + "JOIN c.orders o JOIN c.address a WHERE a.state = 'CA' "
@@ -757,7 +742,6 @@
         assertEquivalence(q, jpql);
     }
 
-    @AllowFailure
     public void testOrdering3() {
         String jpql = "SELECT o.quantity, o.totalCost * 1.08 AS taxedCost, "
             + "a.zipCode FROM Customer c JOIN c.orders o JOIN c.address a "

Modified: openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/CriteriaBuilder.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/CriteriaBuilder.java?rev=785341&r1=785340&r2=785341&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/CriteriaBuilder.java (original)
+++ openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/CriteriaBuilder.java Tue Jun 16 18:56:57 2009
@@ -50,6 +50,7 @@
  * Acts as an adapter to OpenJPA ExpressionFactory.
  * 
  * @author Pinaki Poddar
+ * @author Fay Wang
  * 
  * @since 2.0.0
  *
@@ -445,11 +446,11 @@
     }
 
     public <N extends Number> Expression<N> neg(Expression<N> x) {
-        throw new AbstractMethodError();
+        return new Expressions.Diff<N>(0, x);
     }
 
     public Predicate not(Expression<Boolean> restriction) {
-        throw new AbstractMethodError();
+        return new Expressions.Not(restriction);
     }
 
     public Predicate notEqual(Expression<?> x, Expression<?> y) {
@@ -507,11 +508,12 @@
     }
 
     public <T> Parameter<T> parameter(Class<T> paramClass) {
-        return new ParameterImpl<T>(paramClass);
+        throw new UnsupportedOperationException("Unnamed parameter " +
+                "not supported for CriteriaQuery");
     }
 
     public <T> Parameter<T> parameter(Class<T> paramClass, String name) {
-        return new ParameterImpl<T>(paramClass).setName(name);
+        return new ParameterImpl<T>(paramClass, name);
     }
 
     public <N extends Number> Expression<N> prod(Expression<? extends N> x,
@@ -566,7 +568,8 @@
     }
 
     public <Y> Expression<Y> some(Subquery<Y> subquery) {
-        return new Expressions.Some<Y>(subquery);
+        //some and any are synonymous
+        return new Expressions.Any<Y>(subquery);
     }
 
     public Expression<Double> sqrt(Expression<? extends Number> x) {

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=785341&r1=785340&r2=785341&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 Tue Jun 16 18:56:57 2009
@@ -29,7 +29,7 @@
 import javax.persistence.criteria.Order;
 import javax.persistence.criteria.Root;
 import javax.persistence.criteria.Selection;
-import javax.persistence.metamodel.Entity;
+import javax.persistence.metamodel.Type.PersistenceType;
 
 import org.apache.openjpa.kernel.exps.ExpressionFactory;
 import org.apache.openjpa.kernel.exps.QueryExpressions;
@@ -45,7 +45,6 @@
  * 
  */
 public class CriteriaExpressionBuilder {
-    private int aliasCount = 0;
     
     public QueryExpressions getQueryExpressions(ExpressionFactory factory, 
         CriteriaQueryImpl q) {
@@ -69,8 +68,7 @@
 
         //exps.range = null; // Value[]
         //exps.resultClass = null; // Class
-        if (q.getParameterTypes() != null)
-            exps.parameterTypes = q.getParameterTypes();
+        exps.parameterTypes = q.getParameterTypes();
         return exps;
     }
 
@@ -79,14 +77,21 @@
         Set<ClassMetaData> metas = new HashSet<ClassMetaData>();
         Set<Root<?>> roots = q.getRoots();
         if (roots != null) {
+            MetamodelImpl metamodel = q.getMetamodel();    
             for (Root<?> root : roots) {
-                
                 metas.add(((Types.Managed<?>)root.getModel()).meta);
                 if (root.getJoins() != null) {
                     for (Join<?,?> join : root.getJoins()) {
                         Class<?> cls = join.getMember().getMemberJavaType();
-                        Entity<?> entity = q.getMetamodel().entity(cls);
-                        metas.add(((Types.Managed<?>)entity).meta);
+                        if (join.getMember().isAssociation()) {
+                            ClassMetaData meta = metamodel.repos.
+                                getMetaData(cls, null, true);
+                            PersistenceType type = metamodel.
+                                getPersistenceType(meta);
+                            if (type == PersistenceType.ENTITY ||
+                                type == PersistenceType.EMBEDDABLE) 
+                                metas.add(meta);
+                        }
                     }
                 }
             }
@@ -228,7 +233,7 @@
         //exps.fetchPaths = null;      // String[]
     }
 
-    protected org.apache.openjpa.kernel.exps.Expression and (
+    protected static org.apache.openjpa.kernel.exps.Expression and (
         ExpressionFactory factory,
         org.apache.openjpa.kernel.exps.Expression e1, 
         org.apache.openjpa.kernel.exps.Expression e2) {

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=785341&r1=785340&r2=785341&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 Tue Jun 16 18:56:57 2009
@@ -20,7 +20,10 @@
 
 import java.util.ArrayList;
 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;
@@ -37,9 +40,11 @@
 import javax.persistence.metamodel.Entity;
 
 import org.apache.commons.collections.map.LinkedMap;
+import org.apache.openjpa.kernel.StoreQuery;
 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.QueryImpl;
 import org.apache.openjpa.persistence.meta.MetamodelImpl;
 import org.apache.openjpa.persistence.meta.Types;
 
@@ -61,13 +66,12 @@
     private Set<Root<?>>        _roots;
     private PredicateImpl       _where;
     private List<Order>         _orders;
-    private Set<Parameter<?>>   _params;
+    private LinkedHashMap<Parameter<?>, Class<?>>           _paramTypes;
     private List<Selection<?>>  _selections;
     private List<Expression<?>> _groups;
     private PredicateImpl       _having;
     private List<Subquery<?>>   _subqueries;
     private Boolean             _distinct;
-    private LinkedMap           _parameterTypes;
     private SubqueryImpl<?>     _context;
     
     // AliasContext
@@ -106,8 +110,22 @@
         return _orders;
     }
 
+    /**
+     * Registers the given parameter.
+     * 
+     * @param p
+     */
+    public void registerParameter(ParameterImpl<?> p) {
+        if (_paramTypes == null) {
+            _paramTypes = new LinkedHashMap<Parameter<?>, Class<?>>();
+        }
+        _paramTypes.put(p, p.getJavaType());
+        if (p.getPosition() == null)
+            p.setPosition(_paramTypes.size());
+    }
+    
     public Set<Parameter<?>> getParameters() {
-        return _params;
+        return _paramTypes.keySet();
     }
 
     public List<Selection<?>> getSelectionList() {
@@ -213,11 +231,17 @@
     }
     
     public LinkedMap getParameterTypes() {
-        return _parameterTypes;
-    }
-    
-    public void setParameterTypes(LinkedMap parameterTypes) {
-        _parameterTypes = parameterTypes;
+        if (_paramTypes == null)
+            return StoreQuery.EMPTY_PARAMS;
+        LinkedMap  parameterTypes = new LinkedMap();
+        for (Parameter<?> p : _paramTypes.keySet()) {
+            if (p.getName() == null && p.getPosition() == null)
+                throw new RuntimeException(p + " is not set");
+            Object paramKey = p.getName() == null 
+               ? p.getPosition() : p.getName();
+            parameterTypes.put(paramKey, p.getJavaType());
+        }
+        return parameterTypes;
     }
     
     /**
@@ -271,6 +295,8 @@
      * on the variable and path.  
      */
     public void registerVariable(Selection<?> node, Value var, Value path) {
+        if (isRegistered(node))
+            throw new RuntimeException(node + " is already bound");
         if (!var.isVariable())
             throw new RuntimeException(var.getClass() + " is not a variable");
         if (var.getPath() != path)
@@ -291,8 +317,6 @@
                 throw new RuntimeException("Path alias " + path.getAlias() + 
                 " does not match expected selection alias " + alias);
         }
-        if (isRegistered(node))
-            throw new RuntimeException(node + " is already bound");
         _variables.put(node, var);
         _values.put(node, path);
         _aliases.put(node, alias);

Modified: openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/Expressions.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/Expressions.java?rev=785341&r1=785340&r2=785341&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/Expressions.java (original)
+++ openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/Expressions.java Tue Jun 16 18:56:57 2009
@@ -49,6 +49,12 @@
         MetamodelImpl model, CriteriaQueryImpl q) {
         return (e == null) ? factory.getNull() : e.toValue(factory, model, q);
     }
+     
+     static void setImplicitTypes(Value v1, Value v2, Class<?> expected, 
+             CriteriaQueryImpl q) {
+         JPQLExpressionBuilder.setImplicitTypes(v1, v2, expected, 
+             q.getMetamodel(), q.getParameterTypes(), q.toString());
+     }
     
     /**
      * Unary Functional Expression applies a unary function on a input
@@ -569,11 +575,9 @@
         org.apache.openjpa.kernel.exps.Expression toKernelExpression(
             ExpressionFactory factory, MetamodelImpl model, 
             CriteriaQueryImpl q) {
-            boolean isTypeExpr = false;
             Value val1 = Expressions.toValue(e1, factory, model, q);
             Value val2 = Expressions.toValue(e2, factory, model, q);
-            JPQLExpressionBuilder.setImplicitTypes(val1, val2, null, null, 
-                q.getParameterTypes(), null);
+            Expressions.setImplicitTypes(val1, val2, e1.getJavaType(), q);
             return isNegated() ? factory.notEqual(val1, val2) 
                     : factory.equal(val1, val2);
         }
@@ -594,8 +598,7 @@
             CriteriaQueryImpl q) {
             Value val1 = Expressions.toValue(e1, factory, model, q);
             Value val2 = Expressions.toValue(e2, factory, model, q); 
-            JPQLExpressionBuilder.setImplicitTypes(val1, val2, null, null, 
-                q.getParameterTypes(), null);
+            Expressions.setImplicitTypes(val1, val2, e1.getJavaType(), q); 
             return factory.greaterThan(val1, val2);
         }
     }
@@ -615,8 +618,7 @@
             CriteriaQueryImpl q) {
             Value val1 = Expressions.toValue(e1, factory, model, q);
             Value val2 = Expressions.toValue(e2, factory, model, q); 
-            JPQLExpressionBuilder.setImplicitTypes(val1, val2, null, null, 
-                q.getParameterTypes(), null);
+            Expressions.setImplicitTypes(val1, val2, e1.getJavaType(), q); 
             return factory.greaterThanEqual(val1, val2);
         }
     }
@@ -636,8 +638,7 @@
             CriteriaQueryImpl q) {
             Value val1 = Expressions.toValue(e1, factory, model, q);
             Value val2 = Expressions.toValue(e2, factory, model, q); 
-            JPQLExpressionBuilder.setImplicitTypes(val1, val2, null, null, 
-                ((CriteriaQueryImpl)q).getParameterTypes(), null);
+            Expressions.setImplicitTypes(val1, val2, e1.getJavaType(), q); 
             return factory.lessThan(val1, val2);
         }
     }
@@ -657,8 +658,7 @@
             CriteriaQueryImpl q) {
             Value val1 = Expressions.toValue(e1, factory, model, q);
             Value val2 = Expressions.toValue(e2, factory, model, q); 
-            JPQLExpressionBuilder.setImplicitTypes(val1, val2, null, null, 
-                q.getParameterTypes(), null);
+            Expressions.setImplicitTypes(val1, val2, e1.getJavaType(), q); 
             return factory.lessThanEqual(val1, val2);
         }
     }
@@ -689,33 +689,38 @@
         @Override
         public Value toValue(ExpressionFactory factory, MetamodelImpl model,
             CriteriaQueryImpl q) {
+            Object value = arg;
+            if (arg instanceof ParameterImpl) {
+                return ((ParameterImpl)arg).toValue(factory, model, q);
+            }
             int literalType = Literal.TYPE_UNKNOWN;
             if (arg != null) {
-                Class<?> literalClass = arg.getClass();
-                if (Number.class.isAssignableFrom(literalClass))
+                Class<?> literalClass = value.getClass();
+                if (Number.class.isAssignableFrom(literalClass)) {
                     literalType = Literal.TYPE_NUMBER;
-                else if (Boolean.class.isAssignableFrom(literalClass))
+                } else if (Boolean.class.isAssignableFrom(literalClass)) {
                     literalType = Literal.TYPE_BOOLEAN;
-                else if (String.class.isAssignableFrom(literalClass))
+                } else if (String.class.isAssignableFrom(literalClass)) {
                     literalType = Literal.TYPE_STRING;
-                else if (Enum.class.isAssignableFrom(literalClass))
+                } else if (Enum.class.isAssignableFrom(literalClass)) {
                     literalType = Literal.TYPE_ENUM;
-                else if (Class.class.isAssignableFrom(literalClass)) {
+                } else if (Class.class.isAssignableFrom(literalClass)) {
                     literalType = Literal.TYPE_CLASS;
-                    Literal lit = factory.newTypeLiteral(arg, 
+                    Literal lit = factory.newTypeLiteral(value, 
                         Literal.TYPE_CLASS);
-                    ClassMetaData can = 
-                        ((Types.Entity<X>)q.getRoot().getModel()).meta;
+                    ClassMetaData can = ((Types.Entity<X>)q.getRoot()
+                            .getModel()).meta;
                     Class<?> candidate = can.getDescribedType();
-                    if (candidate.isAssignableFrom((Class)arg))
-                        lit.setMetaData(model.repos.getMetaData((Class<?>)arg, 
-                            null, true));
-                    else
+                    if (candidate.isAssignableFrom((Class)value)) {
+                       lit.setMetaData(model.repos.getMetaData(
+                           (Class<?>)value, null, true));
+                    } else {
                         lit.setMetaData(can);
+                    }
                     return lit;
                 }
             }
-            return factory.newLiteral(arg, literalType);
+            return factory.newLiteral(value, literalType);
         }
     }
     
@@ -1239,18 +1244,22 @@
         }        
     }
 
-    public static class Some<X> extends ExpressionImpl<X> {
-        final SubqueryImpl<X> e;
-        public Some(Subquery<X> x) {
-            super(x.getJavaType());
-            e = (SubqueryImpl<X>)x;
+    public static class Not<X> extends PredicateImpl {
+        protected ExpressionImpl<Boolean> e;
+        public Not(Expression<Boolean> ne) {
+            super();
+            e = (ExpressionImpl<Boolean>)ne;
         }
         
         @Override
-        public Value toValue(ExpressionFactory factory, MetamodelImpl model,
-            CriteriaQueryImpl q) {
-            //return factory.some(Expressions.toValue(e, factory, model, q));
-            return null;
+        public PredicateImpl clone() {
+            return new Not<X>(e);
+        }
+        
+        @Override
+        public org.apache.openjpa.kernel.exps.Expression toKernelExpression(
+          ExpressionFactory factory, MetamodelImpl model, CriteriaQueryImpl q) {
+            return factory.not(super.toKernelExpression(factory, model, q));
         }        
     }
 

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=785341&r1=785340&r2=785341&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 Tue Jun 16 18:56:57 2009
@@ -77,11 +77,11 @@
             if (c.isRegistered(this))
                 return c.getValue(this);
             boolean allowNull = joinType != JoinType.INNER;
+            ClassMetaData meta = _member.fmd.getDeclaredTypeMetaData();
             org.apache.openjpa.kernel.exps.Path path = 
                 (org.apache.openjpa.kernel.exps.Path)
-                getParent().toValue(factory, model, c);
+                _parent.toValue(factory, model, c);
             path.get(_member.fmd, allowNull);
-            ClassMetaData meta = _member.fmd.getDeclaredTypeMetaData();
             path.setMetaData(meta);
             path.setImplicitType(meta.getDescribedType());
             return path;
@@ -91,15 +91,30 @@
         public org.apache.openjpa.kernel.exps.Expression toKernelExpression(
             ExpressionFactory factory, MetamodelImpl model, 
             CriteriaQueryImpl c) {
-            org.apache.openjpa.kernel.exps.Value path = this.toValue
-                (factory, model, c);
             ClassMetaData meta = _member.fmd.getDeclaredTypeMetaData();
+            org.apache.openjpa.kernel.exps.Path path = null;
+            if (c.isRegistered(_parent)) {
+               Value var = c.getVariable(_parent);
+               path = factory.newPath(var);
+               path.setMetaData(meta);
+               path.get(_member.fmd, false);
+            } else
+               path = 
+                (org.apache.openjpa.kernel.exps.Path)toValue(factory, model, c);
             Value var = factory.newBoundVariable(c.getAlias(this), 
                 meta.getDescribedType());
             org.apache.openjpa.kernel.exps.Expression join = factory
                 .bindVariable(var, path);
             c.registerVariable(this, var, path);
-            return join;
+            org.apache.openjpa.kernel.exps.Expression filter = null;
+            if (getJoins() != null) {
+                for (Join<?, ?> join1 : getJoins()) {
+                    filter = CriteriaExpressionBuilder.and(factory, 
+                        ((FromImpl<?,?>)join1).
+                        toKernelExpression(factory, model, c), filter);
+                }
+            }
+            return CriteriaExpressionBuilder.and(factory, join, filter);
         }
     }
     
@@ -153,14 +168,17 @@
         public Value toValue(ExpressionFactory factory, MetamodelImpl model,
             CriteriaQueryImpl c) {
             boolean allowNull = joinType != JoinType.INNER;
-            org.apache.openjpa.kernel.exps.Path path = 
-                (org.apache.openjpa.kernel.exps.Path)
-                _parent.toValue(factory, model, c);
-            path.get(_member.fmd, allowNull);
             ClassMetaData meta = _member.fmd.getElement()
-               .getDeclaredTypeMetaData();
-            path.setMetaData(meta);
-            path.setImplicitType(meta.getDescribedType());
+                .getDeclaredTypeMetaData();
+            org.apache.openjpa.kernel.exps.Path path = null;
+            if (c.isRegistered(this)) {
+                Value var = c.getVariable(this);
+                path = factory.newPath(var);
+            } else {
+                path = (org.apache.openjpa.kernel.exps.Path)
+                    _parent.toValue(factory, model, c);
+                path.get(_member.fmd, allowNull);
+            }
             return path;
         }
 
@@ -172,18 +190,35 @@
         public org.apache.openjpa.kernel.exps.Expression toKernelExpression(
             ExpressionFactory factory, MetamodelImpl model, 
             CriteriaQueryImpl c) {
-            org.apache.openjpa.kernel.exps.Value path = toValue
-               (factory, model, c);
-            
             ClassMetaData meta = _member.fmd.isElementCollection() 
                 ? _member.fmd.getEmbeddedMetaData()
                 : _member.fmd.getElement().getDeclaredTypeMetaData();
                 
+            org.apache.openjpa.kernel.exps.Path path = null;
+            if (c.isRegistered(_parent)) {
+                Value var = c.getVariable(_parent);
+                path = factory.newPath(var);
+                path.setMetaData(meta);
+                path.get(_member.fmd, false);
+            } else           
+                path = (org.apache.openjpa.kernel.exps.Path)
+                    toValue(factory, model, c);
+            
             Value var = factory.newBoundVariable(c.getAlias(this), 
                 meta.getDescribedType());
-            return factory.bindVariable(var, path);
-        }
-        
+            org.apache.openjpa.kernel.exps.Expression join = 
+                factory.bindVariable(var, path);
+            c.registerVariable(this, var, path);
+            org.apache.openjpa.kernel.exps.Expression filter = null;
+            if (getJoins() != null) {
+                for (Join<?, ?> join1 : getJoins()) {
+                    filter = CriteriaExpressionBuilder.and(factory, 
+                        ((FromImpl<?,?>)join1).
+                        toKernelExpression(factory, model, c), filter);
+                }
+            }
+            return CriteriaExpressionBuilder.and(factory, join, filter);
+       }
     }
     
     /**
@@ -285,11 +320,11 @@
         }
         
         public Path<K> key() {
-            throw new AbstractMethodError();
+            return new MapKey<Z,K>(this);
         }
         
         public Path<V> value() {
-            throw new AbstractMethodError();
+            return this;
         }
         
         @Override
@@ -311,4 +346,27 @@
             return join;
         }
     }
+    
+       
+   public static class MapKey<Z,K> extends PathImpl<Z,K> {
+       Map<?,K,?> map;
+       public MapKey(Map<Z,K,?> joinMap){
+           super(((javax.persistence.metamodel.Map<Z, K, ?>)joinMap.getMember())
+                   .getKeyJavaType());
+           this.map = joinMap;
+       }
+       
+       /**
+        * Convert this path to a join expression.
+        * 
+        */
+       @Override
+       public Value toValue(ExpressionFactory factory, MetamodelImpl model, 
+           CriteriaQueryImpl c) {
+           org.apache.openjpa.kernel.exps.Path path = 
+               factory.newPath(c.getVariable(map));
+           return factory.getKey(path);
+       }
+   }
+       
 }

Modified: openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/ParameterImpl.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/ParameterImpl.java?rev=785341&r1=785340&r2=785341&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/ParameterImpl.java (original)
+++ openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/ParameterImpl.java Tue Jun 16 18:56:57 2009
@@ -22,7 +22,6 @@
 
 import javax.persistence.Parameter;
 
-import org.apache.commons.collections.map.LinkedMap;
 import org.apache.openjpa.kernel.exps.ExpressionFactory;
 import org.apache.openjpa.kernel.exps.Value;
 import org.apache.openjpa.meta.ClassMetaData;
@@ -32,64 +31,60 @@
  * Parameter of a criteria query.
  * 
  * @author Pinaki Poddar
- *
+ * @author Fay wang
+ * 
  * @param <T> the type of value held by this parameter.
  */
 public class ParameterImpl<T> extends ExpressionImpl<T> implements Parameter<T>{
 	private String name;
-	private int position;
+	private Integer position;
 	
-    public ParameterImpl(Class<T> cls) {
+    public ParameterImpl(Class<T> cls, String name) {
         super(cls);
+        this.name = name;
     }
 
 	public final String getName() {
 		return name;
 	}
 	
-	public final ParameterImpl<T> setName(String name) {
-		this.name = name;
-		return this;
-	}
-
-	public final Integer getPosition() {
-		return position;
-	}
-
+    public Integer getPosition() {
+        return position;
+    }
+    
+    public void setPosition(int p) {
+        position = p;
+    }
+	
     @Override
     public Value toValue(ExpressionFactory factory, MetamodelImpl model,
         CriteriaQueryImpl q) {
-        boolean positional = false;
-        LinkedMap parameterTypes = ((CriteriaQueryImpl)q).getParameterTypes();
-        if (parameterTypes == null) {
-            parameterTypes = new LinkedMap(6);
-            ((CriteriaQueryImpl)q).setParameterTypes(parameterTypes);
-        } 
-        if (name == null) {
-            position = parameterTypes.size() + 1;
-            positional = true;
-        }
+        q.registerParameter(this);
         
-        Object paramKey = name == null ? Integer.valueOf(position) : name;
-        if (!parameterTypes.containsKey(paramKey))
-            parameterTypes.put(paramKey, Object.class);
 
         ClassMetaData meta = null;
-        Class clzz = getJavaType();
-        int index;
-        if (positional) 
-            index = position - 1;
-        else 
-            // otherwise the index is just the current size of the params
-            index = parameterTypes.indexOf(paramKey);
-        
+        Class<?> clzz = getJavaType();
+        Object paramKey = getKey();
+        int index = getIndex();
         boolean isCollectionValued  = Collection.class.isAssignableFrom(clzz);
         org.apache.openjpa.kernel.exps.Parameter param = isCollectionValued 
-            ? factory.newCollectionValuedParameter(paramKey, Object.class) 
-            : factory.newParameter(paramKey, Object.class);
+            ? factory.newCollectionValuedParameter(paramKey, clzz) 
+            : factory.newParameter(paramKey, clzz);
         param.setMetaData(meta);
         param.setIndex(index);
         
         return param;
     }	
+    
+    Object getKey() {
+        if (name == null && position == null)
+            throw new IllegalStateException(this + " key is not set");
+        return name != null ? name : position;
+    }
+    
+    int getIndex() {
+        if (position == null)
+            throw new IllegalStateException(this + " index is not set");
+        return position-1;
+    }
 }

Modified: openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/PathImpl.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/PathImpl.java?rev=785341&r1=785340&r2=785341&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/PathImpl.java (original)
+++ openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/PathImpl.java Tue Jun 16 18:56:57 2009
@@ -20,6 +20,8 @@
 package org.apache.openjpa.persistence.criteria;
 
 import javax.persistence.criteria.Expression;
+import javax.persistence.criteria.Join;
+import javax.persistence.criteria.JoinType;
 import javax.persistence.criteria.Path;
 import javax.persistence.metamodel.AbstractCollection;
 import javax.persistence.metamodel.Attribute;
@@ -30,6 +32,8 @@
 
 import org.apache.openjpa.kernel.exps.ExpressionFactory;
 import org.apache.openjpa.kernel.exps.Value;
+import org.apache.openjpa.meta.ClassMetaData;
+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;
@@ -47,6 +51,7 @@
 public class PathImpl<Z,X> extends ExpressionImpl<X> implements Path<X> {
     protected final PathImpl<?,Z> _parent;
     protected final Members.Member<? super Z,?> _member;
+    private boolean isEmbedded = false;
     
     /**
      * Protected. use by root path which neither represent a member nor has a
@@ -67,11 +72,17 @@
         Class<X> cls) {
         super(cls);
         _parent = parent;
-        _member = member;
+        if (_parent.isEmbedded) {
+            FieldMetaData fmd = getEmbeddedFieldMetaData(member.fmd);
+            _member = new Members.Attribute(member.owner, fmd);
+        } else 
+            _member = member;
         if (parent == null)
             throw new NullPointerException("Null parent for member " + member);
         if (member == null)
             throw new NullPointerException("Null member for parent " + parent);
+        if (_member.fmd.isEmbedded())
+            isEmbedded = true;
     }
     
     public PathImpl<?,Z> getParentPath() {
@@ -82,6 +93,22 @@
         return (_parent == null) ? this : _parent.getInnermostParentPath();
     }
 
+    protected FieldMetaData getEmbeddedFieldMetaData(FieldMetaData fmd) {
+        Members.Member member = getInnermostMember(_parent,_member);
+        ClassMetaData embeddedMeta = member.fmd.getEmbeddedMetaData();
+        if (embeddedMeta != null)
+            return embeddedMeta.getField(fmd.getName());
+        else
+            return fmd;
+    }
+    
+    protected Members.Member getInnermostMember(PathImpl parent, 
+        Members.Member member) {
+        return member != null ? member : getInnermostMember(parent._parent,
+            parent._member); 
+    }
+    
+    
     /**
      * Convert this path to a kernel path.
      */
@@ -102,11 +129,14 @@
         } else if (_parent != null) { 
             if (q.isRegistered(_parent)) {
                 path = factory.newPath(q.getVariable(_parent));
+                ClassMetaData meta = _member.fmd.getDeclaredTypeMetaData();
+                path.setMetaData(meta);
             } else {
                 path = (org.apache.openjpa.kernel.exps.Path)
                      _parent.toValue(factory, model, q);
             }
-            boolean allowNull = false;
+            boolean allowNull = _parent instanceof Join 
+                    && ((Join<?,?>)_parent).getJoinType() != JoinType.INNER;
             path.get(_member.fmd, allowNull);
         } else {
             path = factory.newPath();

Modified: openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/SubqueryImpl.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/SubqueryImpl.java?rev=785341&r1=785340&r2=785341&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/SubqueryImpl.java (original)
+++ openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/SubqueryImpl.java Tue Jun 16 18:56:57 2009
@@ -160,21 +160,26 @@
     }
     
     public <X,Y> Join<X,Y> correlate(Join<X,Y> join) {
+        _delegate.from(join.getModel().getJavaType());
         return join;
     }
     public <X,Y> CollectionJoin<X,Y> correlate(CollectionJoin<X,Y> join) {
+        _delegate.from(join.getModel().getJavaType());
         return join;
     }
     
     public <X,Y> SetJoin<X,Y> correlate(SetJoin<X,Y> join) {
+        _delegate.from(join.getModel().getJavaType());
         return join;
     }
     
     public <X,Y> ListJoin<X,Y> correlate(ListJoin<X,Y> join) {
+        _delegate.from(join.getModel().getJavaType());
         return join;
     }
     
     public <X,K,V> MapJoin<X,K,V> correlate(MapJoin<X,K,V> join) {
+        _delegate.from(join.getModel().getJavaType());
         return join;
     }
     



Mime
View raw message