openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From curti...@apache.org
Subject svn commit: r1097209 - in /openjpa/trunk: 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 Wed, 27 Apr 2011 19:16:36 GMT
Author: curtisr7
Date: Wed Apr 27 19:16:36 2011
New Revision: 1097209

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

Added:
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cascade/
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cascade/CascadePersistEntity.java
  (with props)
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cascade/TestCascadePersist.java
  (with props)
Modified:
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/conf/Compatibility.java
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/SingleFieldManager.java

Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/conf/Compatibility.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/conf/Compatibility.java?rev=1097209&r1=1097208&r2=1097209&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/conf/Compatibility.java
(original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/conf/Compatibility.java
Wed Apr 27 19:16:36 2011
@@ -66,6 +66,7 @@ public class Compatibility {
     private boolean _isNonDefaultMappingAllowed = false;
     private boolean _reloadOnDetach = false;
     private boolean _ignoreDetachedStateFieldForProxySerialization = false;
+    private boolean _checkDatabaseForCascadePersistToDetachedEntity = false;
     
     /**
      * Whether to require exact identity value types when creating object
@@ -534,5 +535,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/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java?rev=1097209&r1=1097208&r2=1097209&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java (original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java Wed
Apr 27 19:16:36 2011
@@ -4539,8 +4539,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;
 
@@ -4556,6 +4568,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/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/SingleFieldManager.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/SingleFieldManager.java?rev=1097209&r1=1097208&r2=1097209&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/SingleFieldManager.java
(original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/SingleFieldManager.java
Wed Apr 27 19:16:36 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()) { 

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cascade/CascadePersistEntity.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cascade/CascadePersistEntity.java?rev=1097209&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cascade/CascadePersistEntity.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cascade/CascadePersistEntity.java
Wed Apr 27 19:16:36 2011
@@ -0,0 +1,72 @@
+/*
+ * 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.cascade;
+
+import java.io.Serializable;
+
+import javax.persistence.Basic;
+import javax.persistence.CascadeType;
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.OneToOne;
+
+@Entity
+public class CascadePersistEntity {
+
+    @Id
+    long id;
+
+    @OneToOne(cascade = CascadeType.ALL)
+    CascadePersistEntity other;
+
+    @Basic
+    String stringField;
+
+    public CascadePersistEntity() {
+
+    }
+
+    public CascadePersistEntity(long i) {
+        id = i;
+    }
+
+    public String getStringField() {
+        return stringField;
+    }
+
+    public void setStringField(String stringField) {
+        this.stringField = stringField;
+    }
+
+    public long getId() {
+        return id;
+    }
+
+    public void setId(long id) {
+        this.id = id;
+    }
+
+    public CascadePersistEntity getOther() {
+        return other;
+    }
+
+    public void setOther(CascadePersistEntity other) {
+        this.other = other;
+    }
+}

Propchange: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cascade/CascadePersistEntity.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cascade/TestCascadePersist.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cascade/TestCascadePersist.java?rev=1097209&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cascade/TestCascadePersist.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cascade/TestCascadePersist.java
Wed Apr 27 19:16:36 2011
@@ -0,0 +1,71 @@
+/*
+ * 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.cascade;
+
+import javax.persistence.EntityManager;
+import javax.persistence.RollbackException;
+
+import org.apache.openjpa.persistence.test.SingleEMFTestCase;
+
+public class TestCascadePersist extends SingleEMFTestCase {
+    @Override
+    public void setUp() throws Exception {
+        setUp(DROP_TABLES, CascadePersistEntity.class, "openjpa.Log", "SQL=trace");
+    }
+
+    public void testCascadePersistToDetachedFailure() {
+        long startId = System.currentTimeMillis();
+        EntityManager em = emf.createEntityManager();
+        em.getTransaction().begin();
+        CascadePersistEntity cpe1 = new CascadePersistEntity(startId);
+        em.persist(cpe1);
+        em.getTransaction().commit();
+        em.clear();
+
+        em.getTransaction().begin();
+        CascadePersistEntity cpe2 = new CascadePersistEntity(startId + 1);
+        CascadePersistEntity cpe3 = new CascadePersistEntity(startId);
+
+        cpe2.setOther(cpe3);
+        em.persist(cpe2);
+        try {
+            em.getTransaction().commit();
+        } catch (RollbackException re) {
+            // We expect this failure because we are trying to cascade a persist to an existing
Entity. Changing
+            // CheckDatabaseForCascadePersistToDetachedEntity=true would avoid this exception
and revert back to pre
+            // 2.2.x behavior.
+        }
+    }
+
+    public void testCascadePersistToManagedEntity() {
+        long startId = System.currentTimeMillis();
+        EntityManager em = emf.createEntityManager();
+        em.getTransaction().begin();
+        CascadePersistEntity cpe1 = new CascadePersistEntity(startId);
+        em.persist(cpe1);
+
+        em.flush();
+        CascadePersistEntity cpe2 = new CascadePersistEntity(startId + 1);
+
+        cpe2.setOther(cpe1);
+        em.persist(cpe2);
+        // Since cpe1 is managed, it should be ignored by the cascaded persist operation.
+        em.getTransaction().commit();
+    }
+}

Propchange: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cascade/TestCascadePersist.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message