openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From curti...@apache.org
Subject svn commit: r1140182 - in /openjpa/branches/2.1.x: openjpa-kernel/src/main/java/org/apache/openjpa/conf/ openjpa-kernel/src/main/java/org/apache/openjpa/kernel/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cascade/
Date Mon, 27 Jun 2011 14:44:39 GMT
Author: curtisr7
Date: Mon Jun 27 14:44:38 2011
New Revision: 1140182

URL: http://svn.apache.org/viewvc?rev=1140182&view=rev
Log:
OPENJPA-1986: Avoid querying the db when cascading a persist to a new instance.

Added:
    openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cascade/
      - copied from r1097209, openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cascade/
    openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cascade/CascadePersistEntity.java
      - copied unchanged from r1097209, openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cascade/CascadePersistEntity.java
    openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cascade/TestCascadePersist.java
      - copied unchanged from r1097209, openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cascade/TestCascadePersist.java
Modified:
    openjpa/branches/2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/conf/Compatibility.java
    openjpa/branches/2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java
    openjpa/branches/2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/SingleFieldManager.java

Modified: openjpa/branches/2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/conf/Compatibility.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/conf/Compatibility.java?rev=1140182&r1=1140181&r2=1140182&view=diff
==============================================================================
--- openjpa/branches/2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/conf/Compatibility.java
(original)
+++ openjpa/branches/2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/conf/Compatibility.java
Mon Jun 27 14:44:38 2011
@@ -67,6 +67,7 @@ public class Compatibility {
     private boolean _reloadOnDetach = false;
     private boolean _ignoreDetachedStateFieldForProxySerialization = false;
     private boolean _convertPositionalParametersToNamed = false;
+    private boolean _checkDatabaseForCascadePersistToDetachedEntity = false;
     
     /**
      * Whether to require exact identity value types when creating object
@@ -543,5 +544,27 @@ public class Compatibility {
      */
     public void setReloadOnDetach(boolean reloadOnDetach) {
         _reloadOnDetach = reloadOnDetach;
-    }       
+    }      
+    
+    /**
+     * Whether OpenJPA will check the database for an Entity when cascading a persist to
another Entity. This property
+     * only applies for the case where we are trying to cascade a persist to an Entity which
doesn't have a StateManager
+     * and we can't determine if it is detached. 
+     *   
+     * @since 2.1.x
+     */
+    public boolean getCheckDatabaseForCascadePersistToDetachedEntity(){
+        return _checkDatabaseForCascadePersistToDetachedEntity;
+    }
+    
+    /**
+     * Whether OpenJPA will check the database for an Entity when cascading a persist to
another Entity. This property
+     * only applies for the case where we are trying to cascade a persist to an Entity which
doesn't have a StateManager
+     * and we can't determine if it is detached. 
+     *   
+     * @since 2.1.x
+     */
+    public void setCheckDatabaseForCascadePersistToDetachedEntity(boolean b){
+        _checkDatabaseForCascadePersistToDetachedEntity = b;
+    }
 }

Modified: openjpa/branches/2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java?rev=1140182&r1=1140181&r2=1140182&view=diff
==============================================================================
--- openjpa/branches/2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java
(original)
+++ openjpa/branches/2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java
Mon Jun 27 14:44:38 2011
@@ -4527,8 +4527,20 @@ public class BrokerImpl
             return (ImplHelper.toPersistenceCapable(obj, _conf)).pcIsDeleted();
         return false;
     }
-
     public boolean isDetached(Object obj) {
+        return isDetached(obj, true);
+    }
+
+    /**
+     * This method makes a best effort to determine if the provided object is detached.
+     * 
+     * @param obj
+     * @param find
+     *            - If true, as a last resort this method will check whether or not the provided
object exists in the
+     *            DB. If it is in the DB, the provided object is detached.
+     * @return - True if the provided obj is detached, false otherwise.
+     */
+    public boolean isDetached(Object obj, boolean find) {
         if (!(ImplHelper.isManageable(obj)))
             return false;
 
@@ -4544,6 +4556,9 @@ public class BrokerImpl
         Object oid = ApplicationIds.create(pc, meta);
         if (oid == null)
             return false;
+        if(!find){
+            return false;
+        }
         return find(oid, null, EXCLUDE_ALL, null, 0) != null;
     }
 

Modified: openjpa/branches/2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/SingleFieldManager.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/SingleFieldManager.java?rev=1140182&r1=1140181&r2=1140182&view=diff
==============================================================================
--- openjpa/branches/2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/SingleFieldManager.java
(original)
+++ openjpa/branches/2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/SingleFieldManager.java
Mon Jun 27 14:44:38 2011
@@ -29,6 +29,7 @@ import java.util.Date;
 import java.util.Iterator;
 import java.util.Map;
 
+import org.apache.openjpa.conf.Compatibility;
 import org.apache.openjpa.enhance.PersistenceCapable;
 import org.apache.openjpa.lib.util.Localizer;
 import org.apache.openjpa.meta.FieldMetaData;
@@ -49,22 +50,22 @@ import org.apache.openjpa.util.UserExcep
  * FieldManager type used to hold onto a single field value and then
  * dispense it via the fetch methods. The manager can also perform actions
  * on the held field.
- *
- * @author Abe White
  */
-class SingleFieldManager
-    extends TransferFieldManager
-    implements Serializable {
+class SingleFieldManager extends TransferFieldManager implements Serializable {
+    private static final long serialVersionUID = -6205985063476549150L;
 
     private static final Localizer _loc = Localizer.forPackage
         (SingleFieldManager.class);
 
     private final StateManagerImpl _sm;
     private final BrokerImpl _broker;
-
+    private final boolean _checkDbOnCascadePersist; 
+    
     public SingleFieldManager(StateManagerImpl sm, BrokerImpl broker) {
         _sm = sm;
         _broker = broker;
+        _checkDbOnCascadePersist =
+            _broker.getConfiguration().getCompatibilityInstance().getCheckDatabaseForCascadePersistToDetachedEntity();
     }
 
     /**
@@ -756,16 +757,16 @@ class SingleFieldManager
     /**
      * Perform pre flush operations on the given object.
      */
-    private void preFlushPC(ValueMetaData vmd, Object obj, boolean logical,
-        OpCallbacks call) {
+    private void preFlushPC(ValueMetaData vmd, Object obj, boolean logical, OpCallbacks call)
{
         if (obj == null)
             return;
 
         OpenJPAStateManager sm;        
 
         if (vmd.getCascadePersist() == ValueMetaData.CASCADE_NONE) {
-            if (!_broker.isDetachedNew() && _broker.isDetached(obj))
+            if (!_broker.isDetachedNew() && _broker.isDetached(obj, _checkDbOnCascadePersist))
{
                 return; // allow but ignore
+            }
 
             sm = _broker.getStateManager(obj);
             if (sm == null || !sm.isPersistent()) {
@@ -782,8 +783,9 @@ class SingleFieldManager
             }
         } else {
             if (vmd.getCascadePersist() == ValueMetaData.CASCADE_IMMEDIATE) {
-                if (!_broker.isDetachedNew() && _broker.isDetached(obj))
+                if (!_broker.isDetachedNew() && _broker.isDetached(obj, _checkDbOnCascadePersist))
{
                     return; // allow but ignore
+                }
             }        	
             sm = _broker.getStateManager(obj);
             if (sm == null || !sm.isProvisional()) { 



Mime
View raw message