openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hthom...@apache.org
Subject svn commit: r1602032 - in /openjpa/branches/2.2.1.x: ./ openjpa-kernel/src/main/java/org/apache/openjpa/datacache/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/joins/
Date Wed, 11 Jun 2014 22:00:27 GMT
Author: hthomann
Date: Wed Jun 11 22:00:27 2014
New Revision: 1602032

URL: http://svn.apache.org/r1602032
Log:
OPENJPA-2508: Account for JOIN FETCH statements when loading from the Query Cache.  Back ported
to 2.2.1.x Rick's trunk commit.

Modified:
    openjpa/branches/2.2.1.x/   (props changed)
    openjpa/branches/2.2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/QueryCacheStoreQuery.java
    openjpa/branches/2.2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/joins/Department.java
    openjpa/branches/2.2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/joins/Department_.java
    openjpa/branches/2.2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/joins/Employee.java
    openjpa/branches/2.2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/joins/Employee_.java
    openjpa/branches/2.2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/joins/TestJoinFetchWithQueryDataCache.java

Propchange: openjpa/branches/2.2.1.x/
------------------------------------------------------------------------------
  Merged /openjpa/trunk:r1600757

Modified: openjpa/branches/2.2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/QueryCacheStoreQuery.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/QueryCacheStoreQuery.java?rev=1602032&r1=1602031&r2=1602032&view=diff
==============================================================================
--- openjpa/branches/2.2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/QueryCacheStoreQuery.java
(original)
+++ openjpa/branches/2.2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/QueryCacheStoreQuery.java
Wed Jun 11 22:00:27 2014
@@ -112,7 +112,7 @@ public class QueryCacheStoreQuery
      * READ_SERIALIZABLE -- to do so, we'd just return false when in
      * a transaction.
      */
-    private List<Object> checkCache(QueryKey qk) {
+    private List<Object> checkCache(QueryKey qk, FetchConfiguration loadFc) {
         if (qk == null)
             return null;
         FetchConfiguration fetch = getContext().getFetchConfiguration();
@@ -122,9 +122,7 @@ public class QueryCacheStoreQuery
             return null;
 
         // get the cached data
-        QueryResult res = _cache.get(qk);
-
-        
+        QueryResult res = _cache.get(qk);       
         if (res == null) {
             return null;
         }
@@ -165,7 +163,7 @@ public class QueryCacheStoreQuery
                 return null;
             }
         }
-        return new CachedList(res, projs != 0, _sctx);
+        return new CachedList(res, projs != 0, _sctx, loadFc);
     }
 
     /**
@@ -183,7 +181,7 @@ public class QueryCacheStoreQuery
     /**
      * Copy a projection element for caching / returning.
      */
-    private static Object copyProjection(Object obj, StoreContext ctx) {
+    private static Object copyProjection(Object obj, StoreContext ctx, FetchConfiguration
fc) {
         if (obj == null)
             return null;
         switch (JavaTypes.getTypeCode(obj.getClass())) {
@@ -206,7 +204,7 @@ public class QueryCacheStoreQuery
                 return ((Locale) obj).clone();
             default:
                 if (obj instanceof CachedObjectId)
-                    return fromObjectId(((CachedObjectId) obj).oid, ctx);
+                    return fromObjectId(((CachedObjectId) obj).oid, ctx, fc);
                 Object oid = ctx.getObjectId(obj);
                 if (oid != null)
                     return new CachedObjectId(oid);
@@ -217,11 +215,11 @@ public class QueryCacheStoreQuery
     /**
      * Return the result object based on its cached oid.
      */
-    private static Object fromObjectId(Object oid, StoreContext sctx) {
+    private static Object fromObjectId(Object oid, StoreContext sctx, FetchConfiguration
fc) {
         if (oid == null)
             return null;
 
-        Object obj = sctx.find(oid, null, null, null, 0);
+        Object obj = sctx.find(oid, fc, null, null, 0);
         if (obj == null)
             throw new ObjectNotFoundException(oid);
         return obj;
@@ -332,19 +330,34 @@ public class QueryCacheStoreQuery
             _fc = fc;
         }
 
-        public ResultObjectProvider executeQuery(StoreQuery q, Object[] params,
-            Range range) {
+        public ResultObjectProvider executeQuery(StoreQuery q, Object[] params, Range range)
{
             QueryCacheStoreQuery cq = (QueryCacheStoreQuery) q;
             Object parsed = cq.getDelegate().getCompilation();
-            QueryKey key = QueryKey.newInstance(cq.getContext(),
-                _ex.isPacking(q), params, _candidate, _subs, range.start, 
-                range.end, parsed);
-            List<Object> cached = cq.checkCache(key);
-            if (cached != null)
-                return new ListResultObjectProvider(cached);
+            QueryKey key =
+                QueryKey.newInstance(cq.getContext(), _ex.isPacking(q), params, _candidate,
_subs, range.start,
+                    range.end, parsed);
+
+            // Create a new FetchConfiguration that will be used to ensure that any JOIN
FETCHed fields are loaded
+            StoreContext store = q.getContext().getStoreContext();
+            FetchConfiguration cacheFc = store.pushFetchConfiguration();
+            for (QueryExpressions qe : _ex.getQueryExpressions()) {
+                for (String fetchFields : qe.fetchPaths) {
+                    cacheFc.addField(fetchFields);
+                }
+                for (String fetchFields : qe.fetchInnerPaths) {
+                    cacheFc.addField(fetchFields);
+                }
+            }
+            try {
+                List<Object> cached = cq.checkCache(key, cacheFc);
+                if (cached != null) {
+                    return new ListResultObjectProvider(cached);
+                }
+            } finally {
+                store.popFetchConfiguration();
+            }
 
-            ResultObjectProvider rop = _ex.executeQuery(cq.getDelegate(),
-                params, range);
+            ResultObjectProvider rop = _ex.executeQuery(cq.getDelegate(), params, range);
             if (_fc.getQueryCacheEnabled())
                 return cq.wrapResult(rop, key);
             else
@@ -491,7 +504,7 @@ public class QueryCacheStoreQuery
     }
 
     /**
-     * Result list implementation for a cached query result. Package-protected
+     * Result list implementation for a cached query result. Public
      * for testing.
      */
     public static class CachedList extends AbstractList<Object>
@@ -500,23 +513,25 @@ public class QueryCacheStoreQuery
         private final QueryResult _res;
         private final boolean _proj;
         private final StoreContext _sctx;
-
-        public CachedList(QueryResult res, boolean proj, StoreContext ctx) {
+        private final FetchConfiguration _fc;
+        
+        public CachedList(QueryResult res, boolean proj, StoreContext ctx, FetchConfiguration
fc) {
             _res = res;
             _proj = proj;
             _sctx = ctx;
+            _fc = fc;
         }
 
         public Object get(int idx) {
             if (!_proj)
-                return fromObjectId(_res.get(idx), _sctx);
+                return fromObjectId(_res.get(idx), _sctx, _fc);
 
             Object[] cached = (Object[]) _res.get(idx);
             if (cached == null)
                 return null;
             Object[] uncached = new Object[cached.length];
             for (int i = 0; i < cached.length; i++)
-                uncached[i] = copyProjection(cached[i], _sctx);
+                uncached[i] = copyProjection(cached[i], _sctx, _fc);
             return uncached;
         }
 
@@ -601,7 +616,7 @@ public class QueryCacheStoreQuery
                                 Object[] arr = (Object[]) obj;
                                 Object[] cp = new Object[arr.length];
                                 for (int i = 0; i < arr.length; i++)
-                                    cp[i] = copyProjection(arr[i], _sctx);
+                                    cp[i] = copyProjection(arr[i], _sctx, null);
                                 cached = cp;
                             }
                             if (cached != null)

Modified: openjpa/branches/2.2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/joins/Department.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/joins/Department.java?rev=1602032&r1=1602031&r2=1602032&view=diff
==============================================================================
--- openjpa/branches/2.2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/joins/Department.java
(original)
+++ openjpa/branches/2.2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/joins/Department.java
Wed Jun 11 22:00:27 2014
@@ -36,68 +36,70 @@ import javax.persistence.Version;
 @Table(name="FETCHDEPT")
 public class Department implements Serializable {
 
-        private static final long serialVersionUID = -5537435298484817651L;
-
-        @Id
-        private int deptno;
-        @Version
-        private int version;
-        private String name;
-        @OneToMany(cascade=CascadeType.ALL)
-        private List<Employee> employees;
-        @OneToMany(cascade=CascadeType.ALL)
-        private List<Employee> employee2s;
-
-        public Department() {
-                super();
-        }
-
-        public Department(int deptno, String name) {
-                super();
-                this.deptno = deptno;
-                this.name = name;
-        }
-        public int getDeptno() {
-                return this.deptno;
-        }
-
-        public void setDeptno(int deptno) {
-                this.deptno = deptno;
-        }
-
-        public int getVersion() {
-                return this.version;
-        }
-
-        public void setVersion(int version) {
-                this.version = version;
-        }
-
-        public String getName() {
-                return this.name;
-        }
-
-        public void setName(String name) {
-                this.name = name;
-        }
-
-        public List<Employee> getEmployees() {
-                return this.employees;
-        }
-        public void setEmployees(List<Employee> employees) {
-                this.employees = employees;
-        }
-
-        public List<Employee> getEmployee2s() {
-                return this.employee2s;
-        }
-        public void setEmployee2s(List<Employee> employees) {
-                this.employee2s = employees;
-        }
-
-        public String toString() {
-                return "[Department:depno=" + deptno + ", version=" + version + ", name="
+ name +
-                                ", employees=" + employees + ", employee2s=" + employee2s+
']';
-        }
+    private static final long serialVersionUID = -5537435298484817651L;
 
+    @Id
+    private int deptno;
+    @Version
+    private int version;
+    private String name;
+    @OneToMany(mappedBy="dept", cascade=CascadeType.ALL)
+    private List<Employee> employees;
+    @OneToMany(mappedBy="dept", cascade=CascadeType.ALL)
+    private List<Employee> employee2s;
+
+    public Department() {
+        super();
+    }
+
+    public Department(int deptno, String name) {
+        super();
+        this.deptno = deptno;
+        this.name = name;
+    }
+
+    public int getDeptno() {
+        return this.deptno;
+    }
+
+    public void setDeptno(int deptno) {
+        this.deptno = deptno;
+    }
+
+    public int getVersion() {
+        return this.version;
+    }
+
+    public void setVersion(int version) {
+        this.version = version;
+    }
+
+    public String getName() {
+        return this.name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public List<Employee> getEmployees() {
+        return this.employees;
+    }
+
+    public void setEmployees(List<Employee> employees) {
+        this.employees = employees;
+    }
+
+    public List<Employee> getEmployee2s() {
+        return this.employee2s;
+    }
+
+    public void setEmployee2s(List<Employee> employees) {
+        this.employee2s = employees;
+    }
+
+    public String toString() {
+        return "[Department:depno=" + deptno + ", version=" + version + ", name=" + name
+
+                        ", employees=" + employees + ", employee2s=" + employee2s+ ']';
+    }
 }

Modified: openjpa/branches/2.2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/joins/Department_.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/joins/Department_.java?rev=1602032&r1=1602031&r2=1602032&view=diff
==============================================================================
--- openjpa/branches/2.2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/joins/Department_.java
(original)
+++ openjpa/branches/2.2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/joins/Department_.java
Wed Jun 11 22:00:27 2014
@@ -26,7 +26,7 @@ import javax.persistence.metamodel.Singu
 @javax.persistence.metamodel.StaticMetamodel
 (value=org.apache.openjpa.persistence.jpql.joins.Department.class)
 @javax.annotation.Generated
-(value="org.apache.openjpa.persistence.meta.AnnotationProcessor6",date="Sat May 17 17:25:13
CDT 2014")
+(value="org.apache.openjpa.persistence.meta.AnnotationProcessor6",date="Tue Jun 03 09:13:08
CDT 2014")
 public class Department_ {
     public static volatile SingularAttribute<Department,Integer> deptno;
     public static volatile ListAttribute<Department,Employee> employee2s;

Modified: openjpa/branches/2.2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/joins/Employee.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/joins/Employee.java?rev=1602032&r1=1602031&r2=1602032&view=diff
==============================================================================
--- openjpa/branches/2.2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/joins/Employee.java
(original)
+++ openjpa/branches/2.2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/joins/Employee.java
Wed Jun 11 22:00:27 2014
@@ -27,52 +27,65 @@ import javax.persistence.*;
  *
  */
 @Entity
-@Table(name="FETCHEMPL")
+@Table(name = "FETCHEMPL")
 public class Employee implements Serializable {
 
-        private static final long serialVersionUID = -5155314943010802723L;
+    private static final long serialVersionUID = -5155314943010802723L;
 
-        @Id
-        private int empno;
-        private String name;
-        @Version
-        private int version;
-
-        public Employee() {
-                super();
-        }
-
-        public Employee(int empno, String name) {
-                super();
-                this.empno = empno;
-                this.name = name;
-        }
-
-        public int getEmpno() {
-                return this.empno;
-        }
-
-        public void setEmpno(int empno) {
-                this.empno = empno;
-        }
-
-        public int getVersion() {
-                return this.version;
-        }
-
-        public void setVersion(int version) {
-                this.version = version;
-        }
-
-        public String getName() {
-                return this.name;
-        }
-
-        public void setName(String name) {
-                this.name = name;
-        }
-
-        public String toString() {
-                return "[Employee:id=" + empno + ", version=" + version + ", name=" + name
+ ']';
-        }
+    @Id
+    private int empno;
+    private String name;
+    @Version
+    private int version;
+
+    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.MERGE)
+    private Department dept;
+
+    public Employee() {
+        super();
+    }
+
+    public Employee(int empno, String name, Department dept) {
+        super();
+        this.empno = empno;
+        this.name = name;
+        this.dept = dept;
+    }
+
+    public int getEmpno() {
+        return this.empno;
+    }
+
+    public void setEmpno(int empno) {
+        this.empno = empno;
+    }
+
+    public int getVersion() {
+        return this.version;
+    }
+
+    public void setVersion(int version) {
+        this.version = version;
+    }
+
+    public String getName() {
+        return this.name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public Department getDept() {
+        return dept;
+    }
+
+    public void setDept(Department dept) {
+        this.dept = dept;
+    }
+
+    public String toString() {
+        return "[Employee:id=" + empno + ", version=" + version + ", name="
+                + name + ']';
+    }
 }

Modified: openjpa/branches/2.2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/joins/Employee_.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/joins/Employee_.java?rev=1602032&r1=1602031&r2=1602032&view=diff
==============================================================================
--- openjpa/branches/2.2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/joins/Employee_.java
(original)
+++ openjpa/branches/2.2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/joins/Employee_.java
Wed Jun 11 22:00:27 2014
@@ -25,8 +25,9 @@ import javax.persistence.metamodel.Singu
 @javax.persistence.metamodel.StaticMetamodel
 (value=org.apache.openjpa.persistence.jpql.joins.Employee.class)
 @javax.annotation.Generated
-(value="org.apache.openjpa.persistence.meta.AnnotationProcessor6",date="Sat May 17 17:25:28
CDT 2014")
+(value="org.apache.openjpa.persistence.meta.AnnotationProcessor6",date="Tue Jun 03 09:14:37
CDT 2014")
 public class Employee_ {
+    public static volatile SingularAttribute<Employee,Department> dept;
     public static volatile SingularAttribute<Employee,Integer> empno;
     public static volatile SingularAttribute<Employee,String> name;
     public static volatile SingularAttribute<Employee,Integer> version;

Modified: openjpa/branches/2.2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/joins/TestJoinFetchWithQueryDataCache.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/joins/TestJoinFetchWithQueryDataCache.java?rev=1602032&r1=1602031&r2=1602032&view=diff
==============================================================================
--- openjpa/branches/2.2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/joins/TestJoinFetchWithQueryDataCache.java
(original)
+++ openjpa/branches/2.2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/joins/TestJoinFetchWithQueryDataCache.java
Wed Jun 11 22:00:27 2014
@@ -22,6 +22,7 @@ import java.util.ArrayList;
 import java.util.List;
 
 import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
 import javax.persistence.criteria.CriteriaBuilder;
 import javax.persistence.criteria.CriteriaQuery;
 import javax.persistence.criteria.JoinType;
@@ -29,196 +30,217 @@ import javax.persistence.criteria.Root;
 
 import junit.framework.Assert;
 
-import org.apache.openjpa.persistence.test.SingleEMFTestCase;
+import org.apache.openjpa.persistence.OpenJPAEntityManager;
+import org.apache.openjpa.persistence.OpenJPAPersistence;
+import org.apache.openjpa.persistence.test.SQLListenerTestCase;
 
 /**
  * Tests JQPL and Criteria API equivalent using left join fetch with QueryCache and DataCache
enabled.
  */
-public class TestJoinFetchWithQueryDataCache extends SingleEMFTestCase {
+public class TestJoinFetchWithQueryDataCache extends SQLListenerTestCase {
     EntityManager em;
-        public void setUp() {
-        super.setUp(DROP_TABLES, Employee.class, Department.class
-                , "openjpa.QueryCompilationCache", "all"
-                , "openjpa.DataCache", "true(CacheSize=2000, SoftReferenceSize=0, EvictionSchedule='+60'
)"
-                , "openjpa.RemoteCommitProvider", "sjvm"
-                , "openjpa.QueryCache", "true"
-                        );
-
-                em = emf.createEntityManager();
-                em.getTransaction().begin();
-
-                Department dept;
-                dept = new Department(10, "department 10");
-                dept.setEmployees(new ArrayList<Employee>());
-                dept.getEmployees().add(new Employee(11, "Emp11"));
-                dept.getEmployees().add(new Employee(12, "Emp12"));
-                dept.setEmployee2s(new ArrayList<Employee>());
-                dept.getEmployee2s().add(new Employee(211, "Emp211"));
-                dept.getEmployee2s().add(new Employee(212, "Emp212"));
-                em.persist(dept);
-
-                dept = new Department(20, "department 20");
-                dept.setEmployees(new ArrayList<Employee>());
-                dept.getEmployees().add(new Employee(21, "Emp21"));
-                dept.getEmployees().add(new Employee(22, "Emp22"));
-                dept.setEmployee2s(new ArrayList<Employee>());
-                dept.getEmployee2s().add(new Employee(221, "Emp221"));
-                dept.getEmployee2s().add(new Employee(222, "Emp222"));
-                em.persist(dept);
-
-                em.getTransaction().commit();
-
-                em.close();
-        }
-
-        public void testJPQLNoFetch() {
-                EntityManager em = emf.createEntityManager();
-                List<Department> ds = em.createQuery(
-                                "SELECT DISTINCT d FROM Department d WHERE d.deptno = 10")
-                                .getResultList();
-                System.out.println("-- testJPQLNoFetch -----");
-                em.clear();
-                Assert.assertEquals(1, ds.size());
-                for (Department x : ds) {
-                        Assert.assertNull(x.getEmployees());
-                        Assert.assertNull(x.getEmployee2s());
-                        System.out.println(x);
-                }
-
-                em.close();
-        }
-
-        public void testJPQLOneFetch() {
-                EntityManager em = emf.createEntityManager();
-                List<Department> ds = em.createQuery(
-                                "SELECT DISTINCT d FROM Department d LEFT JOIN FETCH d.employee2s
" +
-                                        "WHERE d.deptno = 10")
-                                .getResultList();
-                System.out.println("-- testJPQLOneFetch -----");
-                em.clear();
-                Assert.assertEquals(1, ds.size());
-                for (Department x : ds) {
-                        Assert.assertNull(x.getEmployees());
-                        Assert.assertNotNull(x.getEmployee2s());
-                        Assert.assertEquals(2, x.getEmployee2s().size());
-                        System.out.println(x);
-                }
-
-                em.close();
-        }
-
-        public void testJPQLTwoFetch() {
-                EntityManager em = emf.createEntityManager();
-                List<Department> ds = em.createQuery(
-                                "SELECT DISTINCT d FROM Department d LEFT JOIN FETCH d.employees
" +
-                            "LEFT JOIN FETCH d.employee2s " +
-                                        "WHERE d.deptno = 10")
-                                .getResultList();
-                System.out.println("-- testJPQLTwoFetch -----");
-                em.clear();
-                Assert.assertEquals(1, ds.size());
-                for (Department x : ds) {
-                        Assert.assertNotNull(x.getEmployees());
-                        Assert.assertEquals(2, x.getEmployees().size());
-                        Assert.assertNotNull(x.getEmployee2s());
-                        Assert.assertEquals(2, x.getEmployee2s().size());
-                        System.out.println(x);
-                }
-
-                em.close();
-        }
-
-        public void testCriteriaAPINoFetch() {
-                EntityManager em = emf.createEntityManager();
-                CriteriaBuilder cb = em.getCriteriaBuilder();
-
-                // This query is equivalent to the following Java Persistence query
-                // language query:
-                // SELECT d
-                // FROM Department d
-                // WHERE d.deptno = 1
-
-                CriteriaQuery<Department> q = cb.createQuery(Department.class);
-                Root<Department> d = q.from(Department.class);
-                q.where(cb.equal(d.get(Department_.deptno), 20)).select(d);
-
-                List<Department> ds = em.createQuery(q).getResultList();
-                System.out.println("-- testCriteriaAPINoFetch -----");
-                em.clear();
-                Assert.assertEquals(1, ds.size());
-                for (Department x : ds) {
-                        Assert.assertNull(x.getEmployees());
-                        Assert.assertNull(x.getEmployee2s());
-                        System.out.println(x);
-                }
-
-                em.close();
-        }
-
-        public void testCriteriaAPIOneFetch() {
-                EntityManager em = emf.createEntityManager();
-                CriteriaBuilder cb = em.getCriteriaBuilder();
-
-                // 6.5.4 Fetch Joins
-                // Example:
-                // CriteriaQuery<Department> q = cb.createQuery(Department.class);
-                // Root<Department> d = q.from(Department.class);
-                // d.fetch(Department_.employees, JoinType.LEFT);
-                // q.where(cb.equal(d.get(Department_.deptno), 1)).select(d);
-                //
-                // This query is equivalent to the following Java Persistence query
-                // language query:
-                // SELECT DISTINCT d
-                // FROM Department d LEFT JOIN FETCH d.employees
-                // WHERE d.deptno = 1
-
-                CriteriaQuery<Department> q = cb.createQuery(Department.class);
-                Root<Department> d = q.from(Department.class);
-                d.fetch(Department_.employees, JoinType.LEFT);
-                q.where(cb.equal(d.get(Department_.deptno), 20)).select(d).distinct(true);
-
-                List<Department> ds = em.createQuery(q).getResultList();
-                System.out.println("-- testCriteriaAPIOneFetch -----");
-                em.clear();
-                Assert.assertEquals(1, ds.size());
-                for (Department x : ds) {
-                        Assert.assertNotNull(x.getEmployees());
-                        Assert.assertEquals(2, x.getEmployees().size());
-                        Assert.assertNull(x.getEmployee2s());
-                        System.out.println(x);
-                }
-
-                em.close();
-        }
-
-        public void testCriteriaAPITwoFetch() {
-                EntityManager em = emf.createEntityManager();
-                CriteriaBuilder cb = em.getCriteriaBuilder();
-
-                // This query is equivalent to the following Java Persistence query
-                // language query:
-                // SELECT DISTINCT d
-                // FROM Department d LEFT JOIN FETCH d.employees LEFT JOIN FETCH d.employee2s
-                // WHERE d.deptno = 1
-                CriteriaQuery<Department> q = cb.createQuery(Department.class);
-                Root<Department> d = q.from(Department.class);
-                d.fetch(Department_.employees, JoinType.LEFT);
-                d.fetch(Department_.employee2s, JoinType.LEFT);
-                q.where(cb.equal(d.get(Department_.deptno), 20)).select(d).distinct(true);
-
-                List<Department> ds = em.createQuery(q).getResultList();
-                System.out.println("-- testCriteriaAPITwoFetch -----");
-                em.clear();
-                Assert.assertEquals(1, ds.size());
-                for (Department x : ds) {
-                        Assert.assertNotNull(x.getEmployees());
-                        Assert.assertEquals(2, x.getEmployees().size());
-                        Assert.assertNotNull(x.getEmployee2s());
-                        Assert.assertEquals(2, x.getEmployee2s().size());
-                        System.out.println(x);
-                }
 
-                em.close();
+    public void setUp() {
+        super.setUp(DROP_TABLES, Employee.class, Department.class, "openjpa.QueryCompilationCache",
"all",
+            "openjpa.DataCache", "true", "openjpa.RemoteCommitProvider", "sjvm", "openjpa.QueryCache",
"true"
+            // This is a hack to work around using em.detach(...) w/ a 1.0 p.xml
+            , "openjpa.Compatibility", "CopyOnDetach=false" 
+            );
+
+        em = emf.createEntityManager();
+        em.getTransaction().begin();
+
+        Department dept;
+        dept = new Department(10, "department 10");
+        dept.setEmployees(new ArrayList<Employee>());
+        dept.getEmployees().add(new Employee(11, "Emp11", dept));
+        dept.getEmployees().add(new Employee(12, "Emp12", dept));
+        dept.setEmployee2s(new ArrayList<Employee>());
+        dept.getEmployee2s().add(new Employee(211, "Emp211", dept));
+        dept.getEmployee2s().add(new Employee(212, "Emp212", dept));
+        em.persist(dept);
+
+        dept = new Department(20, "department 20");
+        dept.setEmployees(new ArrayList<Employee>());
+        dept.getEmployees().add(new Employee(21, "Emp21", dept));
+        dept.getEmployees().add(new Employee(22, "Emp22", dept));
+        dept.setEmployee2s(new ArrayList<Employee>());
+        dept.getEmployee2s().add(new Employee(221, "Emp221", dept));
+        dept.getEmployee2s().add(new Employee(222, "Emp222", dept));
+        em.persist(dept);
+
+        em.getTransaction().commit();
+
+        em.close();
+    }
+
+    public void testJPQLNoFetch() {
+        EntityManager em = emf.createEntityManager();
+        List<Department> ds = em.createQuery("SELECT DISTINCT d FROM Department d WHERE
d.deptno = 10").getResultList();
+        System.out.println("-- testJPQLNoFetch -----");
+        em.clear();
+        Assert.assertEquals(1, ds.size());
+        for (Department x : ds) {
+            Assert.assertNull(x.getEmployees());
+            Assert.assertNull(x.getEmployee2s());
+            System.out.println(x);
+        }
+
+        em.close();
+    }
+
+    public void testJPQLOneFetch() {
+        EntityManager em = emf.createEntityManager();
+        List<Department> ds =
+            em.createQuery("SELECT DISTINCT d FROM Department d LEFT JOIN FETCH d.employee2s
" + "WHERE d.deptno = 10")
+                .getResultList();
+        System.out.println("-- testJPQLOneFetch -----");
+        em.clear();
+        Assert.assertEquals(1, ds.size());
+        for (Department x : ds) {
+            Assert.assertNull(x.getEmployees());
+            Assert.assertNotNull(x.getEmployee2s());
+            Assert.assertEquals(2, x.getEmployee2s().size());
+            System.out.println(x);
+        }
+
+        em.close();
+    }
+
+    public void testJPQLTwoFetch() {
+        EntityManager em = emf.createEntityManager();
+        List<Department> ds =
+            em.createQuery(
+                "SELECT DISTINCT d FROM Department d LEFT JOIN FETCH d.employees " + "LEFT
JOIN FETCH d.employee2s "
+                    + "WHERE d.deptno = 10").getResultList();
+        System.out.println("-- testJPQLTwoFetch -----");
+        em.clear();
+        Assert.assertEquals(1, ds.size());
+        for (Department x : ds) {
+            Assert.assertNotNull(x.getEmployees());
+            Assert.assertEquals(2, x.getEmployees().size());
+            Assert.assertNotNull(x.getEmployee2s());
+            Assert.assertEquals(2, x.getEmployee2s().size());
+            System.out.println(x);
+        }
+
+        em.close();
+    }
+
+    public void testCriteriaAPINoFetch() {
+        EntityManager em = emf.createEntityManager();
+        CriteriaBuilder cb = em.getCriteriaBuilder();
+
+        // This query is equivalent to the following Java Persistence query
+        // language query:
+        // SELECT d
+        // FROM Department d
+        // WHERE d.deptno = 1
+
+        CriteriaQuery<Department> q = cb.createQuery(Department.class);
+        Root<Department> d = q.from(Department.class);
+        q.where(cb.equal(d.get(Department_.deptno), 20)).select(d);
+
+        List<Department> ds = em.createQuery(q).getResultList();
+        System.out.println("-- testCriteriaAPINoFetch -----");
+        em.clear();
+        Assert.assertEquals(1, ds.size());
+        for (Department x : ds) {
+            Assert.assertNull(x.getEmployees());
+            Assert.assertNull(x.getEmployee2s());
+            System.out.println(x);
+        }
+
+        em.close();
+    }
+
+    public void testCriteriaAPIOneFetch() {
+        EntityManager em = emf.createEntityManager();
+        CriteriaBuilder cb = em.getCriteriaBuilder();
+
+        // 6.5.4 Fetch Joins
+        // Example:
+        // CriteriaQuery<Department> q = cb.createQuery(Department.class);
+        // Root<Department> d = q.from(Department.class);
+        // d.fetch(Department_.employees, JoinType.LEFT);
+        // q.where(cb.equal(d.get(Department_.deptno), 1)).select(d);
+        //
+        // This query is equivalent to the following Java Persistence query
+        // language query:
+        // SELECT DISTINCT d
+        // FROM Department d LEFT JOIN FETCH d.employees
+        // WHERE d.deptno = 1
+
+        CriteriaQuery<Department> q = cb.createQuery(Department.class);
+        Root<Department> d = q.from(Department.class);
+        d.fetch(Department_.employees, JoinType.LEFT);
+        q.where(cb.equal(d.get(Department_.deptno), 20)).select(d).distinct(true);
+
+        List<Department> ds = em.createQuery(q).getResultList();
+        System.out.println("-- testCriteriaAPIOneFetch -----");
+        em.clear();
+        Assert.assertEquals(1, ds.size());
+        for (Department x : ds) {
+            Assert.assertNotNull(x.getEmployees());
+            Assert.assertEquals(2, x.getEmployees().size());
+            Assert.assertNull(x.getEmployee2s());
+            System.out.println(x);
+        }
+
+        em.close();
+    }
+
+    public void testCriteriaAPITwoFetch() {
+        EntityManager em = emf.createEntityManager();
+        CriteriaBuilder cb = em.getCriteriaBuilder();
+
+        // This query is equivalent to the following Java Persistence query
+        // language query:
+        // SELECT DISTINCT d
+        // FROM Department d LEFT JOIN FETCH d.employees LEFT JOIN FETCH d.employee2s
+        // WHERE d.deptno = 1
+        CriteriaQuery<Department> q = cb.createQuery(Department.class);
+        Root<Department> d = q.from(Department.class);
+        d.fetch(Department_.employees, JoinType.LEFT);
+        d.fetch(Department_.employee2s, JoinType.LEFT);
+        q.where(cb.equal(d.get(Department_.deptno), 20)).select(d).distinct(true);
+
+        List<Department> ds = em.createQuery(q).getResultList();
+        System.out.println("-- testCriteriaAPITwoFetch -----");
+        em.clear();
+        Assert.assertEquals(1, ds.size());
+        for (Department x : ds) {
+            Assert.assertNotNull(x.getEmployees());
+            Assert.assertEquals(2, x.getEmployees().size());
+            Assert.assertNotNull(x.getEmployee2s());
+            Assert.assertEquals(2, x.getEmployee2s().size());
+            System.out.println(x);
+        }
+
+        em.close();
+    }
+
+    public void testConsecutiveJPQLJoinFetchCall() {
+        doQuery(emf, false);
+        doQuery(emf, true);
+    }
+
+    private void doQuery(EntityManagerFactory emf, boolean cached) {
+        String query = "select o from Employee o " + "left join fetch o.dept " + "where o.dept.deptno
= 10";
+        EntityManager em = emf.createEntityManager();
+
+        sql.clear();
+        List<Employee> emps = em.createQuery(query, Employee.class).getResultList();
+        Assert.assertEquals(4, emps.size());
+        for (Employee emp : emps) {
+            em.detach(emp);
+
+            Assert.assertNotNull(emp.getDept());
+            Assert.assertEquals(2, emp.getDept().getEmployees().size());
+        }
+        em.close();
+        if (cached) {
+            assertTrue(sql.size() == 0);
         }
-
+    }
 }



Mime
View raw message