openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rpala...@apache.org
Subject svn commit: r1171927 - in /openjpa/branches/1.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc: kernel/JDBCStoreManager.java meta/strats/StoreCollectionFieldStrategy.java sql/AbstractResult.java sql/MergedResult.java sql/Result.java
Date Sat, 17 Sep 2011 08:26:23 GMT
Author: rpalache
Date: Sat Sep 17 08:26:22 2011
New Revision: 1171927

URL: http://svn.apache.org/viewvc?rev=1171927&view=rev
Log:
OPENJPA-241 Extra SQL on lazy CMR load. Applying FayWang's trunk patch on 1.1.x

Modified:
    openjpa/branches/1.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreManager.java
    openjpa/branches/1.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/StoreCollectionFieldStrategy.java
    openjpa/branches/1.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/AbstractResult.java
    openjpa/branches/1.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/MergedResult.java
    openjpa/branches/1.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/Result.java

Modified: openjpa/branches/1.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreManager.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreManager.java?rev=1171927&r1=1171926&r2=1171927&view=diff
==============================================================================
--- openjpa/branches/1.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreManager.java
(original)
+++ openjpa/branches/1.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreManager.java
Sat Sep 17 08:26:22 2011
@@ -336,6 +336,13 @@ public class JDBCStoreManager 
             // from the indicator is a subclass of expected type
             sm.initialize(type, state);
 
+            if (info != null && info.result != null) {
+                FieldMapping mappedByFieldMapping = info.result.
+                    getMappedByFieldMapping();
+                Object mappedByObject = info.result.getMappedByValue();
+                if (mappedByFieldMapping != null && mappedByObject != null)
+                    setMappedBy(sm, mappedByFieldMapping, mappedByObject);
+            }
             // load the selected mappings into the given state manager
             if (res != null) {
                 // re-get the mapping in case the instance was a subclass
@@ -350,6 +357,18 @@ public class JDBCStoreManager 
         }
     }
     
+    protected void setMappedBy(OpenJPAStateManager sm,
+        FieldMapping mappedByFieldMapping, Object mappedByObject) {
+        ClassMapping mapping = (ClassMapping) sm.getMetaData();
+        FieldMapping[] fms = mapping.getDeclaredFieldMappings();
+        for (int i = 0; i < fms.length; i++) {
+            if (fms[i] == mappedByFieldMapping) {
+                sm.storeObject(fms[i].getIndex(), mappedByObject);
+                return;
+            }
+        }
+    }
+
     /**
      * This method is to provide override for non-JDBC or JDBC-like 
      * implementation of getting version from the result set.

Modified: openjpa/branches/1.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/StoreCollectionFieldStrategy.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/StoreCollectionFieldStrategy.java?rev=1171927&r1=1171926&r2=1171927&view=diff
==============================================================================
--- openjpa/branches/1.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/StoreCollectionFieldStrategy.java
(original)
+++ openjpa/branches/1.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/StoreCollectionFieldStrategy.java
Sat Sep 17 08:26:22 2011
@@ -24,11 +24,13 @@ import java.util.Collection;
 import java.util.HashMap;
 import java.util.Map;
 
+import org.apache.openjpa.enhance.PersistenceCapable;
 import org.apache.openjpa.jdbc.kernel.JDBCFetchConfiguration;
 import org.apache.openjpa.jdbc.kernel.JDBCStore;
 import org.apache.openjpa.jdbc.meta.ClassMapping;
 import org.apache.openjpa.jdbc.meta.FieldMapping;
 import org.apache.openjpa.jdbc.meta.FieldStrategy;
+import org.apache.openjpa.jdbc.meta.ValueMapping;
 import org.apache.openjpa.jdbc.schema.Column;
 import org.apache.openjpa.jdbc.schema.ForeignKey;
 import org.apache.openjpa.jdbc.sql.Joins;
@@ -37,6 +39,7 @@ import org.apache.openjpa.jdbc.sql.Selec
 import org.apache.openjpa.jdbc.sql.SelectExecutor;
 import org.apache.openjpa.jdbc.sql.Union;
 import org.apache.openjpa.kernel.OpenJPAStateManager;
+import org.apache.openjpa.meta.ClassMetaData;
 import org.apache.openjpa.meta.JavaTypes;
 import org.apache.openjpa.util.ChangeTracker;
 import org.apache.openjpa.util.Id;
@@ -293,13 +296,58 @@ public abstract class StoreCollectionFie
 
             if (field.getOrderColumn() != null)
                 seq = res.getInt(field.getOrderColumn(), orderJoins) + 1;
-            add(store, coll, loadElement(null, store, fetch, res, dataJoins));
+
+            // for inverseEager field
+            setMappedBy(oid, sm, coll, res);
+            Object val = loadElement(null, store, fetch, res, dataJoins);
+            add(store, coll, val);
         }
         res.close();
 
         return rels;
     }
 
+    private void setMappedBy(Object oid, OpenJPAStateManager sm, Object coll,
+        Result res) {
+        // for inverseEager field
+        FieldMapping mappedByFieldMapping = field.getMappedByMapping();
+        PersistenceCapable mappedByValue = null;
+        
+        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;
+        	
+            if (oid.equals(sm.getObjectId())) {
+                mappedByValue = sm.getPersistenceCapable();
+                res.setMappedByFieldMapping(mappedByFieldMapping);
+                res.setMappedByValue(mappedByValue);
+            } else if (coll instanceof Collection && 
+                ((Collection) coll).size() > 0) {
+                // Customer (1) <--> Orders(n)
+                // coll contains the values of the toMany field (Orders)
+                // get the StateManager of this toMany value
+                // and find the value of the inverse mappedBy field (Customer)
+                // for this toMacdny field
+                PersistenceCapable pc = (PersistenceCapable)
+                    ((Collection) coll).iterator().next();
+                OpenJPAStateManager sm1 = (OpenJPAStateManager) pc.
+                    pcGetStateManager();
+                
+                ClassMapping clm = ((ClassMapping) sm1.getMetaData());
+                FieldMapping fm = (FieldMapping) clm.getField(
+                    mappedByFieldMapping.getName());
+                if (fm == mappedByFieldMapping)
+                    res.setMappedByValue(sm1.fetchObject(fm.getIndex()));
+            } else {
+                res.setMappedByValue(null);
+            }
+        }        
+    }
+
     /**
      * Extract the oid value from the given result. If the next oid is the
      * same as the given one, returns the given JVM instance.
@@ -476,6 +524,7 @@ public abstract class StoreCollectionFie
             while (res.next()) {
                 if (ct != null && field.getOrderColumn() != null)
                     seq = res.getInt(field.getOrderColumn());
+                setMappedBy(sm.getObjectId(), sm, coll, res);
                 add(store, coll, loadElement(sm, store, fetch, res,
                     resJoins[res.indexOf()]));
             }

Modified: openjpa/branches/1.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/AbstractResult.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/AbstractResult.java?rev=1171927&r1=1171926&r2=1171927&view=diff
==============================================================================
--- openjpa/branches/1.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/AbstractResult.java
(original)
+++ openjpa/branches/1.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/AbstractResult.java
Sat Sep 17 08:26:22 2011
@@ -81,6 +81,8 @@ public abstract class AbstractResult
     private boolean _locking = false;
     private boolean _ignoreNext = false;
     private boolean _last = false;
+    private FieldMapping _mappedByFieldMapping = null;
+    private Object _mappedByValue = null;
 
     public Object getEager(FieldMapping key) {
         Map map = getEagerMap(true);
@@ -119,6 +121,8 @@ public abstract class AbstractResult
      */
     public void close() {
         closeEagerMap(_eager);
+        _mappedByFieldMapping = null;
+        _mappedByValue = null;
     }
 
     /**
@@ -239,6 +243,22 @@ public abstract class AbstractResult
         _base = base;
     }
 
+    public FieldMapping getMappedByFieldMapping() {
+        return (_gotEager) ? null : _mappedByFieldMapping;
+    }
+
+    public void setMappedByFieldMapping(FieldMapping fieldMapping) {
+        _mappedByFieldMapping = fieldMapping;
+    }
+
+    public Object getMappedByValue() {
+        return (_gotEager) ? null : _mappedByValue;
+    }
+
+    public void setMappedByValue(Object mappedByValue) {
+        _mappedByValue = mappedByValue;
+    }
+
     public int indexOf() {
         return _index;
     }

Modified: openjpa/branches/1.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/MergedResult.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/MergedResult.java?rev=1171927&r1=1171926&r2=1171927&view=diff
==============================================================================
--- openjpa/branches/1.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/MergedResult.java
(original)
+++ openjpa/branches/1.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/MergedResult.java
Sat Sep 17 08:26:22 2011
@@ -211,6 +211,22 @@ public class MergedResult
         _res[_idx].setBaseMapping(mapping);
     }
 
+    public FieldMapping getMappedByFieldMapping() {
+        return _res[_idx].getMappedByFieldMapping();
+    }
+
+    public void setMappedByFieldMapping(FieldMapping fieldMapping) {
+        _res[_idx].setMappedByFieldMapping(fieldMapping);
+    }
+
+    public Object getMappedByValue() {
+        return _res[_idx].getMappedByValue();
+    }
+
+    public void setMappedByValue(Object mappedByValue) {
+        _res[_idx].setMappedByValue(mappedByValue);
+    }
+
     public int indexOf() {
         return _res[_idx].indexOf();
     }

Modified: openjpa/branches/1.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/Result.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/Result.java?rev=1171927&r1=1171926&r2=1171927&view=diff
==============================================================================
--- openjpa/branches/1.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/Result.java
(original)
+++ openjpa/branches/1.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/Result.java
Sat Sep 17 08:26:22 2011
@@ -161,6 +161,36 @@ public interface Result
     public void setBaseMapping(ClassMapping mapping);
 
     /**
+     * If this is the result used to select a toMany relationship,
+     * the mappedByFieldMapping is field mapping representing 
+     * the inverse relationship. This is to avoid unneeded  
+     * extra sql to retrieve the eager inverse field.
+     */
+    public FieldMapping getMappedByFieldMapping();
+
+    /**
+     * If this is the result used to select a toMany relationship,
+     * the mappedByFieldMapping is field mapping representing 
+     * the inverse relationship. This is to avoid unneeded  
+     * extra sql to retrieve the eager inverse field.
+     */
+    public void setMappedByFieldMapping(FieldMapping fieldMapping);
+
+    /**
+     * If this is the result used to select a toMany relationship,
+     * the mappedByValue is value of the owner of the toMany relationship. 
+     * This is to avoid unneeded extra sql to retrieve the eager inverse field.
+     */
+    public Object getMappedByValue();
+
+    /**
+     * If this is the result used to select a toMany relationship,
+     * the mappedByValue is value of the owner of the toMany relationship. 
+     * This is to avoid unneeded extra sql to retrieve the eager inverse field.
+     */
+    public void setMappedByValue(Object mappedByValue);
+
+    /**
      * The index of the select within the UNION that the current row
      * corresponds to, or 0.
      */



Mime
View raw message