openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fa...@apache.org
Subject svn commit: r713857 - in /openjpa/branches/1.2.x: openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/ openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/
Date Thu, 13 Nov 2008 23:15:53 GMT
Author: fancy
Date: Thu Nov 13 15:15:53 2008
New Revision: 713857

URL: http://svn.apache.org/viewvc?rev=713857&view=rev
Log:
OPENJPA-744 Extra SQL on LAZY/EAGER  ManyToOne relation
Apply fix to 1.2.x branch

Added:
    openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/OneManyEagerChild.java
    openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/OneManyEagerParent.java
    openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/OneManyLazyChild.java
    openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/TestManyEagerSQL.java
Modified:
    openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreManager.java
    openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/StoreCollectionFieldStrategy.java
    openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/TestInverseEagerSQL.java

Modified: openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreManager.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreManager.java?rev=713857&r1=713856&r2=713857&view=diff
==============================================================================
--- openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreManager.java
(original)
+++ openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreManager.java
Thu Nov 13 15:15:53 2008
@@ -35,6 +35,7 @@
 
 import javax.sql.DataSource;
 
+import org.apache.openjpa.enhance.PersistenceCapable;
 import org.apache.openjpa.event.OrphanedKeyAction;
 import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
 import org.apache.openjpa.jdbc.conf.QuerySQLCacheValue;
@@ -354,7 +355,19 @@
                     getMappedByFieldMapping();
                 Object mappedByObject = info.result.getMappedByValue();
                 if (mappedByFieldMapping != null && mappedByObject != null)
-                    setMappedBy(sm, mappedByFieldMapping, mappedByObject);
+                    if (mappedByObject instanceof OpenJPAId)
+                        // The inverse relation can not be set since
+                        // we are eagerly loading this sm for
+                        // a sm owner that is still in the process of 
+                        // initializing itself.
+                        // Remember owner oid by setIntermediate().
+                        // The inverse relation is set later by
+                        // setInverseRelation() when the sm owner is fully
+                        // initialized.
+                        sm.setIntermediate(mappedByFieldMapping.getIndex(),
+                            mappedByObject);
+                    else
+                        setMappedBy(sm, mappedByFieldMapping, mappedByObject);
             }
             // load the selected mappings into the given state manager
             if (res != null) {
@@ -362,6 +375,7 @@
                 mapping = (ClassMapping) sm.getMetaData();
                 load(mapping, sm, fetch, res);
                 getVersion(mapping, sm, res);
+                setInverseRelation(sm, mapping, res);
             }
             return true;
         } finally {
@@ -370,6 +384,48 @@
         }
     }
 
+    private void setInverseRelation(OpenJPAStateManager owner,
+        ClassMapping mapping, Result res) {
+        FieldMapping[] fms = mapping.getFieldMappings();
+
+        // At this point, the owner is fully initialized.
+        // Check if the owner has eagerly loaded ToMany relations.
+        for (int i = 0; i < fms.length; i++) {
+            if (res.getEager(fms[i]) != null) {
+                Object coll =  owner.fetchObject(fms[i].getIndex());
+                if (coll instanceof Collection && 
+                    ((Collection) coll).size() > 0) {
+                    // Found eagerly loaded collection.
+                    // Publisher (1) <==>  (M) Magazine
+                    //    publisher has a EAGER OneToMany relation
+                    //    magazine has a EAGER or LAZY ManyToOne publisher
+                    // For each member (Magazine) in the collection, 
+                    // set its inverse relation (Publisher).
+                    for (Iterator itr = ((Collection) coll).iterator();
+                        itr.hasNext();) {
+                        PersistenceCapable pc = (PersistenceCapable) itr.next();
+                        OpenJPAStateManager sm = (OpenJPAStateManager) pc.
+                            pcGetStateManager();
+                        FieldMapping[] fmd = ((ClassMapping) sm.getMetaData()).
+                            getFieldMappings();
+                        for (int j = 0; j < fmd.length; j++) {
+                            Object oid = sm.getIntermediate(fmd[j].getIndex());
+                            // if oid was setIntermediate() previously
+                            // and it is the same as the owner,
+                            // then set the inverse relation
+                            if (oid != null &&
+                                oid.equals(owner.getObjectId())) {
+                                sm.storeObject(fmd[j].getIndex(),
+                                    owner.getPersistenceCapable());
+                                break;
+                            }
+                        }
+                    }
+                }
+            }
+        }
+    }
+
     protected void setMappedBy(OpenJPAStateManager sm,
         FieldMapping mappedByFieldMapping, Object mappedByObject) {
         ClassMapping mapping = (ClassMapping) sm.getMetaData();
@@ -914,9 +970,29 @@
         ConnectionInfo info = new ConnectionInfo();
         info.result = result;
         info.mapping = mapping;
+
+        // if inverse relation is known, exclude loading during find
+        exclude = excludeInverseRelation(mapping, info, exclude);
         return _ctx.find(oid, fetch, exclude, info, 0);
     }
 
+    private BitSet excludeInverseRelation(ClassMapping mapping,
+        ConnectionInfo info, BitSet exclude) {
+        FieldMapping inverse = info.result.getMappedByFieldMapping();
+        if (inverse != null) {
+            FieldMapping[] fms = mapping.getDefinedFieldMappings();
+            if (exclude == null)
+                exclude = new BitSet(fms.length);
+            for (int i = 0; i < fms.length; i++) {
+                if (fms[i] == inverse) {
+                    exclude.set(fms[i].getIndex());
+                    break;
+                }
+            }
+        }
+        return exclude;
+    }
+
     /**
      * Load the given state manager with data from the result set. Only
      * mappings originally selected will be loaded.

Modified: openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/StoreCollectionFieldStrategy.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/StoreCollectionFieldStrategy.java?rev=713857&r1=713856&r2=713857&view=diff
==============================================================================
--- openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/StoreCollectionFieldStrategy.java
(original)
+++ openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/StoreCollectionFieldStrategy.java
Thu Nov 13 15:15:53 2008
@@ -312,8 +312,9 @@
             if (field.getOrderColumn() != null)
                 seq = res.getInt(field.getOrderColumn(), orderJoins) + 1;
 
-            // for inverseEager field
-            setMappedBy(oid, sm, coll, res);
+            // for inverse relation field
+            setMappedBy(oid.equals(sm.getObjectId()) ? 
+                sm.getPersistenceCapable() : oid, res);
             Object val = loadElement(null, store, fetch, res, dataJoins);
             add(store, coll, val);
         }
@@ -322,6 +323,23 @@
         return rels;
     }
 
+    private void setMappedBy(Object oid, Result res) {
+        //  for inverse toOne relation field
+        FieldMapping mappedByFieldMapping = field.getMappedByMapping();
+        
+        if (mappedByFieldMapping != null) {
+            ValueMapping val = mappedByFieldMapping.getValueMapping();
+            ClassMetaData decMeta = val.getTypeMetaData();
+            // this inverse field does not have corresponding classMapping
+            // its value may be a collection/map etc.
+            if (decMeta == null) 
+                return;
+            
+            res.setMappedByFieldMapping(mappedByFieldMapping);
+            res.setMappedByValue(oid);
+        }
+    }
+
     private void setMappedBy(Object oid, OpenJPAStateManager sm, Object coll,
         Result res) {
         // for inverseEager field

Added: openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/OneManyEagerChild.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/OneManyEagerChild.java?rev=713857&view=auto
==============================================================================
--- openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/OneManyEagerChild.java
(added)
+++ openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/OneManyEagerChild.java
Thu Nov 13 15:15:53 2008
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+ */
+package org.apache.openjpa.persistence.relations;
+
+import javax.persistence.Entity;
+import javax.persistence.FetchType;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.JoinColumn;
+import javax.persistence.ManyToOne;
+import javax.persistence.Version;
+
+@Entity
+public class OneManyEagerChild {
+
+    @Id
+    @GeneratedValue
+    private long id;
+
+    private String name;
+
+    @ManyToOne(fetch=FetchType.EAGER)
+    @JoinColumn(name="PARENT_ID")
+    private OneManyEagerParent parent;
+
+    @Version
+    private Integer optLock;
+
+    public long getId() { 
+        return id; 
+    }
+
+    public String getName() { 
+        return name; 
+    }
+
+    public void setName(String name) { 
+        this.name = name; 
+    }
+
+    public OneManyEagerParent getParent() { 
+        return parent; 
+    }
+
+    public void setParent(OneManyEagerParent parent) { 
+        this.parent = parent; 
+    }
+}

Added: openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/OneManyEagerParent.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/OneManyEagerParent.java?rev=713857&view=auto
==============================================================================
--- openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/OneManyEagerParent.java
(added)
+++ openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/OneManyEagerParent.java
Thu Nov 13 15:15:53 2008
@@ -0,0 +1,83 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+ */
+package org.apache.openjpa.persistence.relations;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.persistence.Entity;
+import javax.persistence.FetchType;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.OneToMany;
+import javax.persistence.OrderBy;
+import javax.persistence.Version;
+
+@Entity
+public class OneManyEagerParent {
+
+    @Id
+    @GeneratedValue
+    private long id;
+
+    private String name;
+
+    @OneToMany(mappedBy="parent", fetch=FetchType.EAGER)
+    @OrderBy("name ASC")
+    private List<OneManyLazyChild> lazychildren = 
+        new ArrayList<OneManyLazyChild>();
+
+    @OneToMany(mappedBy="parent", fetch=FetchType.EAGER)
+    @OrderBy("name ASC")
+    private List<OneManyEagerChild> eagerchildren = 
+        new ArrayList<OneManyEagerChild>();
+
+    @Version
+    private Integer optLock;
+
+    public long getId() { 
+        return id; 
+    }
+
+    public List<OneManyLazyChild> getLazyChildren() { 
+        return lazychildren; 
+    }
+
+    public void addLazyChild(OneManyLazyChild child) {
+        child.setParent(this);
+        lazychildren.add(child);
+    }
+
+    public List<OneManyEagerChild> getEagerChildren() { 
+        return eagerchildren; 
+    }
+
+    public void addEagerChild(OneManyEagerChild child) {
+        child.setParent(this);
+        eagerchildren.add(child);
+    }
+
+    public String getName() { 
+        return name; 
+    }
+
+    public void setName(String name) { 
+        this.name = name; 
+    }
+}

Added: openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/OneManyLazyChild.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/OneManyLazyChild.java?rev=713857&view=auto
==============================================================================
--- openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/OneManyLazyChild.java
(added)
+++ openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/OneManyLazyChild.java
Thu Nov 13 15:15:53 2008
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+ */
+package org.apache.openjpa.persistence.relations;
+
+import javax.persistence.Entity;
+import javax.persistence.FetchType;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.JoinColumn;
+import javax.persistence.ManyToOne;
+import javax.persistence.Version;
+
+@Entity
+public class OneManyLazyChild {
+
+    @Id
+    @GeneratedValue
+    private long id;
+
+    private String name;
+
+    @ManyToOne(fetch=FetchType.LAZY)
+    @JoinColumn(name="PARENT_ID")
+    private OneManyEagerParent parent;
+
+    @Version
+    private Integer optLock;
+
+    public long getId() { 
+        return id; 
+    }
+
+    public String getName() { 
+        return name; 
+    }
+
+    public void setName(String name) { 
+        this.name = name; 
+    }
+
+    public OneManyEagerParent getParent() { 
+        return parent; 
+    }
+
+    public void setParent(OneManyEagerParent parent) { 
+        this.parent = parent; 
+    }
+}

Modified: openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/TestInverseEagerSQL.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/TestInverseEagerSQL.java?rev=713857&r1=713856&r2=713857&view=diff
==============================================================================
--- openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/TestInverseEagerSQL.java
(original)
+++ openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/TestInverseEagerSQL.java
Thu Nov 13 15:15:53 2008
@@ -21,12 +21,16 @@
 import java.util.Collection;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Set;
+
 import javax.persistence.EntityManager;
 import javax.persistence.Query;
 
 import junit.textui.TestRunner;
 import org.apache.openjpa.persistence.OpenJPAEntityManager;
 import org.apache.openjpa.persistence.OpenJPAQuery;
+import org.apache.openjpa.persistence.query.Magazine;
+import org.apache.openjpa.persistence.query.Publisher;
 import org.apache.openjpa.persistence.test.SQLListenerTestCase;
 
 
@@ -36,7 +40,8 @@
     public void setUp() {
         setUp(Customer.class, Customer.CustomerKey.class, Order.class, 
         	EntityAInverseEager.class, EntityA1InverseEager.class, EntityA2InverseEager.class,

-        	EntityBInverseEager.class, EntityCInverseEager.class, EntityDInverseEager.class);
+        	EntityBInverseEager.class, EntityCInverseEager.class, EntityDInverseEager.class,
+            Publisher.class, Magazine.class);
         
         EntityManager em = emf.createEntityManager();
         em.getTransaction().begin();
@@ -93,7 +98,29 @@
             c1.setD(d1);
             d1.setC(c1);
         }
-        
+
+        Publisher p1 = new Publisher();
+        p1.setName("publisher1");
+        em.persist(p1);
+   
+        for (int i = 0; i < 4; i++) {
+            Magazine magazine = new Magazine();
+            magazine.setIdPublisher(p1);
+            magazine.setName("magagine"+i+"_"+p1.getName());
+            em.persist(magazine);
+        }
+
+        Publisher p2 = new Publisher();
+        p2.setName("publisher2");
+        em.persist(p2);
+   
+        for (int i = 0; i < 4; i++) {
+            Magazine magazine = new Magazine();
+            magazine.setIdPublisher(p2);
+            magazine.setName("magagine"+i+"_"+p2.getName());
+            em.persist(magazine);
+        }
+
         em.flush();
         em.getTransaction().commit();
         em.close();
@@ -197,6 +224,33 @@
         em.close();
     }
 
+    public void testOneToManyEagerInverseLazyQuery() {
+        sql.clear();
+
+        OpenJPAEntityManager em = emf.createEntityManager();
+        String query = "select p FROM Publisher p";
+        Query q = em.createQuery(query);
+        List list = q.getResultList();
+        assertEquals(2, list.size());
+        assertEquals(2, sql.size());
+
+        sql.clear();
+        em.clear();
+        for (int i = 0; i < list.size(); i++) {
+            Publisher p = (Publisher) list.get(i);
+            Set<Magazine> magazines = p.getMagazineCollection();
+            assertEquals(4, magazines.size());
+            for (Iterator iter = magazines.iterator(); iter.hasNext();) {
+                Magazine m = (Magazine) iter.next();
+                Publisher mp = m.getIdPublisher();
+                assertEquals(p, mp);
+            }
+        }
+
+        assertEquals(0, sql.size());
+        em.close();
+    }
+
     public static void main(String[] args) {
         TestRunner.run(TestInverseEagerSQL.class);
     }

Added: openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/TestManyEagerSQL.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/TestManyEagerSQL.java?rev=713857&view=auto
==============================================================================
--- openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/TestManyEagerSQL.java
(added)
+++ openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/TestManyEagerSQL.java
Thu Nov 13 15:15:53 2008
@@ -0,0 +1,197 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+ */
+package org.apache.openjpa.persistence.relations;
+
+import java.util.List;
+
+import javax.persistence.EntityManager;
+import javax.persistence.Query;
+
+import junit.textui.TestRunner;
+
+import org.apache.openjpa.persistence.OpenJPAEntityManager;
+import org.apache.openjpa.persistence.test.SQLListenerTestCase;
+
+
+public class TestManyEagerSQL
+    extends SQLListenerTestCase {
+
+    public void setUp() {
+        setUp(
+            OneManyEagerParent.class, OneManyEagerChild.class,
+            OneManyLazyChild.class);
+        
+        EntityManager em = emf.createEntityManager();
+        em.getTransaction().begin();
+
+        for (int j = 0; j < 2; j++) {
+            OneManyEagerParent parent = new OneManyEagerParent();
+            parent.setName("parent"+j);
+            for (int i = 0; i < 2; i++) {
+                OneManyEagerChild child = new OneManyEagerChild();
+                child.setName("eagerchild" + i);
+                parent.addEagerChild(child);
+                em.persist(child);
+                OneManyLazyChild lazychild = new OneManyLazyChild();
+                lazychild.setName("lazychild" + i);
+                parent.addLazyChild(lazychild);
+                em.persist(lazychild);
+            }
+            em.persist(parent);
+        }
+
+        em.flush();
+        em.getTransaction().commit();
+        em.close();
+    }
+
+    public void testManyToOneEagerQuery() {
+        sql.clear();
+
+        OpenJPAEntityManager em = emf.createEntityManager();
+        String query = "select c FROM OneManyEagerChild c";
+        Query q = em.createQuery(query);
+        List list = q.getResultList();
+        assertEquals(4, list.size());
+        
+        // Expected SQLs:
+        //   SELECT t0.id, t0.optLock, t0.name, t1.id, t1.optLock, t1.name
+        //       FROM OneManyEagerChild t0
+        //       LEFT OUTER JOIN OneManyEagerParent t1 ON t0.PARENT_ID = t1.id
+        //   SELECT t0.id, t0.optLock, t0.name FROM OneManyEagerChild t0
+        //       WHERE t0.PARENT_ID = ? ORDER BY t0.name ASC [params=(long) 2]
+        //   SELECT t0.id, t0.optLock, t0.name, t0.PARENT_ID
+        //       FROM OneManyLazyChild t0 WHERE t0.PARENT_ID = ?
+        //       ORDER BY t0.name ASC [params=(long) 2]
+        //   SELECT t0.id, t0.optLock, t0.name FROM OneManyEagerChild t0
+        //       WHERE t0.PARENT_ID = ? ORDER BY t0.name ASC [params=(long) 1]
+        //   SELECT t0.id, t0.optLock, t0.name, t0.PARENT_ID 
+        //       FROM OneManyLazyChild t0 WHERE t0.PARENT_ID = ?
+        //       ORDER BY t0.name ASC [params=(long) 1]
+
+        assertEquals(5, sql.size());
+
+        sql.clear();
+
+        for (int i = 0; i < list.size(); i++) {
+            OneManyEagerChild child = (OneManyEagerChild) list.get(i);
+            assertEquals(2, child.getParent().getLazyChildren().size());
+            assertEquals(2, child.getParent().getEagerChildren().size());
+        }
+        assertEquals(0, sql.size());
+
+        em.close();
+    }
+
+    public void testManyToOneLazyQuery() {
+        sql.clear();
+
+        OpenJPAEntityManager em = emf.createEntityManager();
+        String query = "select c FROM OneManyLazyChild c";
+        Query q = em.createQuery(query);
+        List list = q.getResultList();
+        assertEquals(4, list.size());
+
+        // Expected SQL:
+        // SELECT t0.id, t0.optLock, t0.name, t0.PARENT_ID
+        //     FROM OneManyLazyChild t0
+        assertEquals(1, sql.size());
+
+        sql.clear();
+
+        for (int i = 0; i < list.size(); i++) {
+            OneManyLazyChild child = (OneManyLazyChild) list.get(i);
+            assertEquals(2, child.getParent().getLazyChildren().size());
+            assertEquals(2, child.getParent().getEagerChildren().size());
+        }
+
+        // Expected SQLs: (fired on child.getParent())
+        //  SELECT t0.optLock, t0.name, t1.PARENT_ID, t1.id, t1.optLock, t1.name
+        //      FROM OneManyEagerParent t0
+        //      LEFT OUTER JOIN OneManyEagerChild t1 ON t0.id = t1.PARENT_ID
+        //      WHERE t0.id = ? 
+        //      ORDER BY t1.PARENT_ID ASC, t1.name ASC [params=(long) 252]
+        //  SELECT t0.id, t0.optLock, t0.name, t0.PARENT_ID
+        //      FROM OneManyLazyChild t0 WHERE t0.PARENT_ID = ?
+        //      ORDER BY t0.name ASC [params=(long) 252]
+        //  SELECT t0.optLock, t0.name, t1.PARENT_ID, t1.id, t1.optLock, t1.name
+        //      FROM OneManyEagerParent t0
+        //      LEFT OUTER JOIN OneManyEagerChild t1 ON t0.id = t1.PARENT_ID
+        //      WHERE t0.id = ? 
+        //      ORDER BY t1.PARENT_ID ASC, t1.name ASC [params=(long) 251]
+        //  SELECT t0.id, t0.optLock, t0.name, t0.PARENT_ID
+        //      FROM OneManyLazyChild t0 WHERE t0.PARENT_ID = ?
+        //      ORDER BY t0.name ASC [params=(long) 251]
+
+        assertEquals(4, sql.size());
+
+        em.close();
+    }
+
+    public void testOneToManyEagerQuery() {
+        sql.clear();
+
+        OpenJPAEntityManager em = emf.createEntityManager();
+        String query = "select p FROM OneManyEagerParent p";
+        Query q = em.createQuery(query);
+        List list = q.getResultList();
+        assertEquals(2, list.size());
+
+        // Expected SQLs:
+        //   SELECT t0.id, t0.optLock, t0.name FROM OneManyEagerParent t0
+        //   SELECT t0.id, t1.id, t1.optLock, t1.name FROM OneManyEagerParent t0
+        //       INNER JOIN OneManyEagerChild t1 ON t0.id = t1.PARENT_ID
+        //       ORDER BY t0.id ASC, t1.name ASC
+        //   SELECT t0.id, t1.id, t1.optLock, t1.name, t1.PARENT_ID
+        //       FROM OneManyEagerParent t0
+        //       INNER JOIN OneManyLazyChild t1 ON t0.id = t1.PARENT_ID
+        //       ORDER BY t0.id ASC, t1.name ASC
+
+        assertEquals(3, sql.size());
+
+        sql.clear();
+
+        for (int i = 0; i < list.size(); i++) {
+            OneManyEagerParent p = (OneManyEagerParent) list.get(i);
+            long id = p.getId();
+            assertEquals(2, p.getEagerChildren().size());
+            assertEquals(p, p.getEagerChildren().get(0).getParent());
+            assertEquals(p, p.getEagerChildren().get(1).getParent());
+            assertEquals(id, p.getEagerChildren().get(0).getParent().getId());
+            assertEquals(id, p.getEagerChildren().get(1).getParent().getId());
+            assertEquals("eagerchild0", p.getEagerChildren().get(0).getName());
+            assertEquals("eagerchild1", p.getEagerChildren().get(1).getName());
+            assertEquals(2, p.getLazyChildren().size());
+            assertEquals(p, p.getLazyChildren().get(0).getParent());
+            assertEquals(p, p.getLazyChildren().get(1).getParent());
+            assertEquals(id, p.getLazyChildren().get(0).getParent().getId());
+            assertEquals(id, p.getLazyChildren().get(1).getParent().getId());
+            assertEquals("lazychild0", p.getLazyChildren().get(0).getName());
+            assertEquals("lazychild1", p.getLazyChildren().get(1).getName());           

+        }
+
+        assertEquals(0, sql.size());
+        em.close();
+    }
+
+    public static void main(String[] args) {
+        TestRunner.run(TestManyEagerSQL.class);
+    }
+}
+



Mime
View raw message