openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From curti...@apache.org
Subject svn commit: r1095401 - in /openjpa/trunk: openjpa-kernel/src/main/java/org/apache/openjpa/kernel/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/merge/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/merge/m...
Date Wed, 20 Apr 2011 13:27:56 GMT
Author: curtisr7
Date: Wed Apr 20 13:27:56 2011
New Revision: 1095401

URL: http://svn.apache.org/viewvc?rev=1095401&view=rev
Log:
OPENJPA-1983: Don't cascade to an unloaded field when merge is called on a managed Entity.

Added:
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/merge/TestLazyFields.java
  (with props)
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/merge/model/Inner.java
  (with props)
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/merge/model/Outer.java
  (with props)
Modified:
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AttachManager.java

Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AttachManager.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AttachManager.java?rev=1095401&r1=1095400&r2=1095401&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AttachManager.java
(original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AttachManager.java
Wed Apr 20 13:27:56 2011
@@ -19,6 +19,7 @@
 package org.apache.openjpa.kernel;
 
 import java.util.ArrayList;
+import java.util.BitSet;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Iterator;
@@ -37,11 +38,11 @@ import org.apache.openjpa.meta.FieldMeta
 import org.apache.openjpa.meta.ValueMetaData;
 import org.apache.openjpa.util.CallbackException;
 import org.apache.openjpa.util.Exceptions;
+import org.apache.openjpa.util.ImplHelper;
 import org.apache.openjpa.util.OpenJPAException;
 import org.apache.openjpa.util.OptimisticException;
 import org.apache.openjpa.util.ProxyManager;
 import org.apache.openjpa.util.UserException;
-import org.apache.openjpa.util.ImplHelper;
 
 /**
  * Handles attaching instances.
@@ -59,7 +60,7 @@ public class AttachManager {
     private final boolean _copyNew;
     private final boolean _failFast;
     private final IdentityMap _attached = new IdentityMap();
-    private final Collection _visitedNodes = new ArrayList();
+    private final Collection<StateManagerImpl> _visitedNodes = new ArrayList();
 
     // reusable strategies
     private AttachStrategy _version;
@@ -253,17 +254,18 @@ public class AttachManager {
     }
 
     private Object handleCascade(Object toAttach, OpenJPAStateManager owner) {
-        FieldMetaData[] fields = _broker.getStateManager(toAttach).getMetaData()
-            .getDefinedFields();
-        for (int i = 0; i < fields.length; i++) {
-            FieldMetaData fd = (FieldMetaData) fields[i];
-            if (fd.getElement().getCascadeAttach() == fd.CASCADE_IMMEDIATE) {
-                FieldMetaData[] inverseFieldMappings = fd.getInverseMetaDatas();
+        StateManagerImpl sm = _broker.getStateManagerImpl(toAttach, true);
+        BitSet loaded = sm.getLoaded();
+        FieldMetaData[] fmds = sm.getMetaData().getDefinedFields();
+        for (FieldMetaData fmd : fmds) {
+            if (fmd.getElement().getCascadeAttach() == ValueMetaData.CASCADE_IMMEDIATE) {
+                FieldMetaData[] inverseFieldMappings = fmd.getInverseMetaDatas();
                 if (inverseFieldMappings.length != 0) {
-                    OpenJPAStateManager sm = _broker.getStateManager(toAttach);
                     _visitedNodes.add(sm);
-                    getStrategy(toAttach).attachField(this, toAttach,
-                        _broker.getStateManagerImpl(toAttach, true), fd, true);
+                    // Only try to attach this field is it is loaded
+                    if (loaded.get(fmd.getIndex())) {
+                        getStrategy(toAttach).attachField(this, toAttach, sm, fmd, true);
+                    }
                 }
             }
         }

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/merge/TestLazyFields.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/merge/TestLazyFields.java?rev=1095401&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/merge/TestLazyFields.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/merge/TestLazyFields.java
Wed Apr 20 13:27:56 2011
@@ -0,0 +1,82 @@
+/*
+ * 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.merge;
+
+import java.util.ArrayList;
+
+import javax.persistence.EntityManager;
+
+import org.apache.openjpa.persistence.merge.model.Inner;
+import org.apache.openjpa.persistence.merge.model.Outer;
+import org.apache.openjpa.persistence.test.SingleEMFTestCase;
+
+public class TestLazyFields extends SingleEMFTestCase {
+    public void setUp() {
+        setUp(CLEAR_TABLES, Outer.class, Inner.class);
+    }
+
+    public void testMergeOfLazyFields() {
+        EntityManager em = emf.createEntityManager();
+        try {
+            em.getTransaction().begin();
+            Outer o1 = new Outer();
+            Inner i1 = new Inner();
+            o1.setInners(new ArrayList<Inner>());
+            o1.getInners().add(i1);
+
+            em.persist(o1);
+
+            em.getTransaction().commit();
+            em.clear(); // the objects will now get detached.
+
+            long id = o1.getId();
+
+            em.getTransaction().begin();
+            Outer o2 = em.find(Outer.class, id);
+
+            // Since o2 is in the context, it should be ignored... but the merge will needs
to be cascaded
+            // to loaded fields.
+            Outer mergedO2 = em.merge(o2);
+            
+            // Make sure that the merge operation didn't return a different outer.
+            assertEquals(mergedO2, o2);
+
+            em.getTransaction().commit();
+            em.clear();
+
+            // Fetch again
+            em.getTransaction().begin();
+            Outer o3 = em.find(Outer.class, id);
+
+            // We're checking that the merge didn't cascade to the unloaded field and wipe
out all Inners
+            assertTrue(o3.getInners().size() > 0);
+            
+            em.getTransaction().commit();
+            em.clear();
+
+        } finally {
+            if (em != null) {
+                if (em.getTransaction().isActive()) {
+                    em.getTransaction().rollback();
+                }
+                em.close();
+            }
+        }
+    }
+}

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

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/merge/model/Inner.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/merge/model/Inner.java?rev=1095401&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/merge/model/Inner.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/merge/model/Inner.java
Wed Apr 20 13:27:56 2011
@@ -0,0 +1,70 @@
+/*
+ * 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.merge.model;
+
+import java.util.ArrayList;
+import java.util.Collection;
+
+import javax.persistence.CascadeType;
+import javax.persistence.Entity;
+import javax.persistence.FetchType;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.ManyToMany;
+import javax.persistence.Table;
+
+@Entity
+@Table(name = "INNER_TBL")
+public class Inner {
+
+    @Id
+    @GeneratedValue(strategy = GenerationType.IDENTITY)
+    private Long id = null;
+
+    private String name = null;
+
+    @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "inners")
+    private Collection<Outer> outers = new ArrayList<Outer>();
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public Long getId() {
+        return id;
+    }
+
+    public void setId(Long id) {
+        this.id = id;
+    }
+
+    public Collection<Outer> getOuters() {
+        return outers;
+    }
+
+    public void setOuters(Collection<Outer> outers) {
+        this.outers = outers;
+    }
+
+}

Propchange: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/merge/model/Inner.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/merge/model/Outer.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/merge/model/Outer.java?rev=1095401&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/merge/model/Outer.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/merge/model/Outer.java
Wed Apr 20 13:27:56 2011
@@ -0,0 +1,69 @@
+/*
+ * 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.merge.model;
+
+import java.util.List;
+
+import javax.persistence.CascadeType;
+import javax.persistence.Entity;
+import javax.persistence.FetchType;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.ManyToMany;
+import javax.persistence.Table;
+
+@Entity
+@Table(name = "OUTER_TBL")
+public class Outer {
+
+    @Id
+    @GeneratedValue(strategy = GenerationType.IDENTITY)
+    private Long id = null;
+
+    private String name = null;
+
+    @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
+    private List<Inner> inners = null;
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public Long getId() {
+        return id;
+    }
+
+    public void setId(Long id) {
+        this.id = id;
+    }
+
+    public List<Inner> getInners() {
+        return inners;
+    }
+
+    public void setInners(List<Inner> inners) {
+        this.inners = inners;
+    }
+
+}

Propchange: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/merge/model/Outer.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message