openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jrba...@apache.org
Subject svn commit: r903812 - in /openjpa/trunk: openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/util/ openjpa-persistence/src/main/java/org/apache/openjpa/persistence/
Date Wed, 27 Jan 2010 19:58:00 GMT
Author: jrbauer
Date: Wed Jan 27 19:57:59 2010
New Revision: 903812

URL: http://svn.apache.org/viewvc?rev=903812&view=rev
Log:
OPENJPA-1485 Check persistent collection attributes for null when determining load state.

Added:
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/util/OneToEntity.java
  (with props)
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/util/ToManyEager.java
  (with props)
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/util/ToManyLazy.java
  (with props)
Modified:
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/util/TestPersistenceUnitUtil.java
    openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/OpenJPAPersistenceUtil.java

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/util/OneToEntity.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/util/OneToEntity.java?rev=903812&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/util/OneToEntity.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/util/OneToEntity.java
Wed Jan 27 19:57:59 2010
@@ -0,0 +1,67 @@
+/*
+ * 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.util;
+
+import java.util.Collection;
+
+import javax.persistence.CascadeType;
+import javax.persistence.Entity;
+import javax.persistence.FetchType;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.OneToMany;
+
+@Entity
+public class OneToEntity {
+
+    @Id
+    @GeneratedValue
+    private int id;
+
+    @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY)
+    private Collection<ToManyLazy> toManyLazy;
+
+    @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
+    private Collection<ToManyEager> toManyEager;
+
+    public void setId(int id) {
+        this.id = id;
+    }
+
+    public int getId() {
+        return id;
+    }
+
+    public void setToManyLazy(Collection<ToManyLazy> toManyEnt) {
+        this.toManyLazy = toManyEnt;
+    }
+
+    public Collection<ToManyLazy> getToManyLazy() {
+        return toManyLazy;
+    }
+
+    public void setToManyEager(Collection<ToManyEager> toManyEnt) {
+        this.toManyEager = toManyEnt;
+    }
+
+    public Collection<ToManyEager> getToManyEager() {
+        return toManyEager;
+    }
+
+}

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

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/util/TestPersistenceUnitUtil.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/util/TestPersistenceUnitUtil.java?rev=903812&r1=903811&r2=903812&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/util/TestPersistenceUnitUtil.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/util/TestPersistenceUnitUtil.java
Wed Jan 27 19:57:59 2010
@@ -21,6 +21,7 @@
 import java.sql.Date;
 import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Random;
 
 import javax.persistence.EntityManager;
@@ -43,7 +44,8 @@
         setUp(CLEAR_TABLES, EagerEntity.class, LazyEmbed.class,
             LazyEntity.class, EagerEmbed.class, RelEntity.class,
             EagerEmbedRel.class, MapEntity.class,
-            MapKeyEmbed.class, MapValEntity.class);
+            MapKeyEmbed.class, MapValEntity.class,
+            OneToEntity.class, ToManyLazy.class, ToManyEager.class);
     }
 
     /*
@@ -251,7 +253,7 @@
         em.close();
     }
 
-    public void testPCMapEager() {        
+    public void testPCMapEager() {
         PersistenceUnitUtil puu = emf.getPersistenceUnitUtil();
         EntityManager em = emf.createEntityManager();
         
@@ -296,6 +298,133 @@
         em.close();
     }
 
+    /*
+     * Verify load state is not loaded for null relationships or relationships
+     * set to null.
+     */
+    public void testSetNullLazyRelationship() {
+
+        PersistenceUnitUtil puu = emf.getPersistenceUnitUtil();
+        EntityManager em = emf.createEntityManager();
+
+        try {
+            OneToEntity ote = new OneToEntity();
+            assertFalse(puu.isLoaded(ote, "toManyLazy"));
+            em.getTransaction().begin();
+            em.persist(ote);
+            em.getTransaction().commit();
+            em.clear();
+            ote = em.find(OneToEntity.class, ote.getId());
+            // Field is lazy and not immediately loaded by the application
+            assertFalse(puu.isLoaded(ote, "toManyLazy"));
+            // Force load the lazy field
+            ote.getToManyLazy();
+            assertTrue(puu.isLoaded(ote, "toManyLazy"));
+            
+            OneToEntity ote2 = new OneToEntity();
+            em.getTransaction().begin();
+            em.persist(ote2);
+            em.getTransaction().commit();
+            // Field gets set to loaded upon commit
+            assertTrue(puu.isLoaded(ote2, "toManyLazy"));
+            em.clear();
+            ote2 = em.find(OneToEntity.class, ote2.getId());
+            
+            // Field is lazy and not immediately loaded by the application
+            assertFalse(puu.isLoaded(ote2, "toManyLazy"));
+            
+            // Load by application
+            List<ToManyLazy> tmes = new ArrayList<ToManyLazy>();
+            for (int i = 0; i < 5; i++) {
+                tmes.add(new ToManyLazy("ToMany" + i));
+            }
+            em.getTransaction().begin();
+            ote2.setToManyLazy(tmes);
+            // App loaded before commit
+            assertTrue(puu.isLoaded(ote2, "toManyLazy"));
+            em.getTransaction().commit();
+            // Still loaded after commit
+            assertTrue(puu.isLoaded(ote2, "toManyLazy"));
+            
+            // Set to null - no longer loaded per spec.
+            em.getTransaction().begin();
+            ote2.setToManyLazy(null);
+            // Considered unloaded before commit
+            assertFalse(puu.isLoaded(ote2, "toManyLazy"));
+            em.getTransaction().commit();
+            //Loaded after commit
+            assertTrue(puu.isLoaded(ote2, "toManyLazy"));
+        }
+        finally {
+            if (em.getTransaction().isActive()) {
+                em.getTransaction().rollback();
+            }
+        }
+        em.close();
+    }
+
+    public void testSetNullEagerRelationship() {
+
+        PersistenceUnitUtil puu = emf.getPersistenceUnitUtil();
+        EntityManager em = emf.createEntityManager();
+
+        try {
+            OneToEntity ote = new OneToEntity();
+            assertFalse(puu.isLoaded(ote, "toManyEager"));
+            em.getTransaction().begin();
+            em.persist(ote);
+            em.getTransaction().commit();
+            em.clear();
+            ote = em.find(OneToEntity.class, ote.getId());
+            // Field is eater and is immediately loaded by the application
+            assertTrue(puu.isLoaded(ote, "toManyEager"));
+            
+            OneToEntity ote2 = new OneToEntity();
+            em.getTransaction().begin();
+            em.persist(ote2);
+            // Field is null by default and not considered loaded.
+            assertFalse(puu.isLoaded(ote2, "toManyEager"));
+            em.getTransaction().commit();
+            // Field gets set to loaded upon commit
+            assertTrue(puu.isLoaded(ote2, "toManyEager"));
+            em.clear();
+            ote2 = em.find(OneToEntity.class, ote2.getId());
+            
+            // Field is eager and is immediately loaded by the application
+            assertTrue(puu.isLoaded(ote2, "toManyEager"));
+            
+            // Load by application
+            List<ToManyEager> tmes = new ArrayList<ToManyEager>();
+            for (int i = 0; i < 5; i++) {
+                tmes.add(new ToManyEager("ToMany" + i));
+            }
+            em.getTransaction().begin();
+            ote2.setToManyEager(tmes);
+            // App loaded before commit
+            assertTrue(puu.isLoaded(ote2, "toManyEager"));
+            em.getTransaction().commit();
+            // Still loaded after commit
+            assertTrue(puu.isLoaded(ote2, "toManyEager"));
+            
+            // Set to null - no longer loaded per spec.
+            em.getTransaction().begin();
+            ote2.setToManyEager(null);
+            // Entity is considered unloaded before commit
+            assertFalse(puu.isLoaded(ote2));
+            // Attribute is considered unloaded before commit
+            assertFalse(puu.isLoaded(ote2, "toManyEager"));
+            em.getTransaction().commit();
+            //Loaded after commit
+            assertTrue(puu.isLoaded(ote2, "toManyEager"));
+        }
+        finally {
+            if (em.getTransaction().isActive()) {
+                em.getTransaction().rollback();
+            }
+        }
+        em.close();
+    }
+
     private EagerEntity createEagerEntity() {
         EagerEntity ee = new EagerEntity();
         ee.setId(new Random().nextInt());

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/util/ToManyEager.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/util/ToManyEager.java?rev=903812&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/util/ToManyEager.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/util/ToManyEager.java
Wed Jan 27 19:57:59 2010
@@ -0,0 +1,57 @@
+/*
+ * 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.util;
+
+import javax.persistence.Id;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+
+@Entity
+public class ToManyEager {
+
+    public ToManyEager() {
+    }
+
+    public ToManyEager(String n) {
+        setName(n);
+    }
+
+    @Id
+    @GeneratedValue
+    private int id;
+    
+    private String name;
+
+    public void setId(int id) {
+        this.id = id;
+    }
+
+    public int getId() {
+        return id;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public String getName() {
+        return name;
+    }
+}

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

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/util/ToManyLazy.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/util/ToManyLazy.java?rev=903812&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/util/ToManyLazy.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/util/ToManyLazy.java
Wed Jan 27 19:57:59 2010
@@ -0,0 +1,56 @@
+/*
+ * 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.util;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+
+@Entity
+public class ToManyLazy {
+
+    public ToManyLazy() {
+    }
+
+    public ToManyLazy(String n) {
+        setName(n);
+    }
+
+    @Id
+    @GeneratedValue
+    private int id;
+    
+    private String name;
+
+    public void setId(int id) {
+        this.id = id;
+    }
+
+    public int getId() {
+        return id;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public String getName() {
+        return name;
+    }
+}

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

Modified: openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/OpenJPAPersistenceUtil.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/OpenJPAPersistenceUtil.java?rev=903812&r1=903811&r2=903812&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/OpenJPAPersistenceUtil.java
(original)
+++ openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/OpenJPAPersistenceUtil.java
Wed Jan 27 19:57:59 2010
@@ -150,6 +150,11 @@
                 if(!loadSet.get(fmd.getIndex())) {
                     return LoadState.NOT_LOADED;
                 }
+                // If a collected valued attribute and it has been modified, 
+                // make sure it isn't null
+                if (isCollectionSetToNull(sm, fmd)) {
+                    return LoadState.NOT_LOADED;
+                }
             }
             FieldMetaData[] fmds = sm.getMetaData().getFields();
             // Check load state of all persistent eager fetch attributes
@@ -172,7 +177,29 @@
         }
         return isLoaded ? LoadState.LOADED : LoadState.NOT_LOADED;        
     }
+
+    /*
+     * Returns true if the field is a collection type and it was explicitly
+     * set to null.
+     */
+    private static boolean isCollectionSetToNull(OpenJPAStateManager sm, FieldMetaData fmd)
{
+        BitSet dirtySet = sm.getDirty();
+        if (dirtySet.get(fmd.getIndex()) && isCollectionType(fmd.getDeclaredTypeCode()))
{ 
+            Object field = sm.fetchField(fmd.getIndex(), false);
+            if (field == null) {
+                return true;
+            }
+        }
+        return false;
+    }
+    
     
+    private static boolean isCollectionType(int type) {
+        return (type == JavaTypes.COLLECTION ||
+                type == JavaTypes.MAP ||
+                type == JavaTypes.ARRAY);
+    }
+
     private static HashSet<OpenJPAStateManager> addToLoadSet(
         HashSet<OpenJPAStateManager> pcs, OpenJPAStateManager sm) {
         if (pcs == null) {
@@ -202,14 +229,14 @@
         // If a collection type, determine if it is loaded
         switch (fmd.getDeclaredTypeCode()) {
             case JavaTypes.COLLECTION:   
-                return isLoadedCollection(sm, fmd.getElement(), 
-                    (Collection<?>)field, pcs);
+                return !isCollectionSetToNull(sm, fmd) && 
+                    isLoadedCollection(sm, fmd.getElement(),(Collection<?>)field, pcs);
             case JavaTypes.MAP:
-                return isLoadedMap(sm, fmd, 
-                    (Map<?,?>)field, pcs);
+                return !isCollectionSetToNull(sm, fmd) &&
+                    isLoadedMap(sm, fmd, (Map<?,?>)field, pcs);
             case JavaTypes.ARRAY:
-                return isLoadedArray(sm, fmd.getElement(), 
-                    (Object[])field, pcs);
+                return !isCollectionSetToNull(sm, fmd) &&
+                    isLoadedArray(sm, fmd.getElement(), (Object[])field, pcs);
         }
         // If other PC type, determine if it is loaded
         if (ofsm != null && fmd.isDeclaredTypePC()) {



Mime
View raw message