You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by aw...@apache.org on 2006/09/26 19:39:21 UTC

svn commit: r450122 - in /incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detachment: ./ DetachmentOneManyChild.java DetachmentOneManyParent.java TestDetachmentOneMany.java

Author: awhite
Date: Tue Sep 26 10:39:21 2006
New Revision: 450122

URL: http://svn.apache.org/viewvc?view=rev&rev=450122
Log:
Add David Ezzio's detachment test.


Added:
    incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detachment/
    incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detachment/DetachmentOneManyChild.java   (with props)
    incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detachment/DetachmentOneManyParent.java   (with props)
    incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detachment/TestDetachmentOneMany.java   (with props)

Added: incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detachment/DetachmentOneManyChild.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detachment/DetachmentOneManyChild.java?view=auto&rev=450122
==============================================================================
--- incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detachment/DetachmentOneManyChild.java (added)
+++ incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detachment/DetachmentOneManyChild.java Tue Sep 26 10:39:21 2006
@@ -0,0 +1,63 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed 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.detachment;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.JoinColumn;
+import javax.persistence.ManyToOne;
+import javax.persistence.Version;
+
+import org.apache.openjpa.persistence.jdbc.ForeignKey;
+
+@Entity
+public class DetachmentOneManyChild {
+
+    @Id
+    @GeneratedValue
+    private long id;
+
+    private String name;
+
+    @ManyToOne(optional=false)
+    @JoinColumn(name="PARENT_ID", nullable=false)
+    @ForeignKey
+    private DetachmentOneManyParent parent;
+
+    @Version
+    private Integer optLock;
+
+    public long getId() { 
+        return id; 
+    }
+
+    public String getName() { 
+        return name; 
+    }
+
+    public void setName(String name) { 
+        this.name = name; 
+    }
+
+    public DetachmentOneManyParent getParent() { 
+        return parent; 
+    }
+
+    public void setParent(DetachmentOneManyParent parent) { 
+        this.parent = parent; 
+    }
+}

Propchange: incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detachment/DetachmentOneManyChild.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detachment/DetachmentOneManyParent.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detachment/DetachmentOneManyParent.java?view=auto&rev=450122
==============================================================================
--- incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detachment/DetachmentOneManyParent.java (added)
+++ incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detachment/DetachmentOneManyParent.java Tue Sep 26 10:39:21 2006
@@ -0,0 +1,60 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed 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.detachment;
+
+import java.util.ArrayList;
+import java.util.List;
+import javax.persistence.*;
+
+@Entity
+public class DetachmentOneManyParent {
+
+    @Id
+    @GeneratedValue
+    private long id;
+
+    private String name;
+
+    @OneToMany(mappedBy="parent", cascade=CascadeType.ALL, 
+        fetch=FetchType.EAGER)
+    @OrderBy("name ASC")
+    private List<DetachmentOneManyChild> children = 
+        new ArrayList<DetachmentOneManyChild>();
+
+    @Version
+    private Integer optLock;
+
+    public long getId() { 
+        return id; 
+    }
+
+    public List<DetachmentOneManyChild> getChildren() { 
+        return children; 
+    }
+
+    public void addChild(DetachmentOneManyChild child) {
+        child.setParent(this);
+        children.add(child);
+    }
+
+    public String getName() { 
+        return name; 
+    }
+
+    public void setName(String name) { 
+        this.name = name; 
+    }
+}

Propchange: incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detachment/DetachmentOneManyParent.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detachment/TestDetachmentOneMany.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detachment/TestDetachmentOneMany.java?view=auto&rev=450122
==============================================================================
--- incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detachment/TestDetachmentOneMany.java (added)
+++ incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detachment/TestDetachmentOneMany.java Tue Sep 26 10:39:21 2006
@@ -0,0 +1,145 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed 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.detachment;
+
+import java.util.HashMap;
+import java.util.Map;
+import javax.persistence.EntityManager;
+import javax.persistence.Persistence;
+
+import junit.framework.TestCase;
+import junit.textui.TestRunner;
+import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory;
+import org.apache.openjpa.persistence.OpenJPAPersistence;
+import org.apache.openjpa.kernel.AutoDetach;
+
+/**
+ * Tests detachment for bidirectional one-many relationship
+ *
+ * @author David Ezzio
+ */
+public class TestDetachmentOneMany
+    extends TestCase {
+
+    private OpenJPAEntityManagerFactory emf;
+
+    @SuppressWarnings("unchecked")
+    public void setUp() {
+        String types = DetachmentOneManyParent.class.getName() + ";"
+            + DetachmentOneManyChild.class.getName(); 
+        Map props = new HashMap();
+        props.put("openjpa.MetaDataFactory", "jpa(Types=" + types + ")");
+        emf = (OpenJPAEntityManagerFactory) Persistence.
+            createEntityManagerFactory("test", props);
+    }
+
+    public void tearDown() {
+        if (emf == null)
+            return;
+        try {
+            EntityManager em = emf.createEntityManager();
+            em.getTransaction().begin();
+            em.createQuery("delete from DetachmentOneManyChild").
+                executeUpdate();
+            em.createQuery("delete from DetachmentOneManyParent").
+                executeUpdate();
+            em.getTransaction().commit();
+            em.close();
+            emf.close();
+        } catch (Exception e) {
+        }
+    }
+    
+    public void testDetachment() {
+        long id = createParentAndChildren();
+    
+        EntityManager em = emf.createEntityManager();
+        OpenJPAPersistence.cast(em).setAutoDetach(AutoDetach.DETACH_NONTXREAD);
+        DetachmentOneManyParent parent = em.find(DetachmentOneManyParent.class,
+            id);
+        assertNotNull(parent);
+        assertFalse("The parent was not detached", em.contains(parent));
+    }
+
+    public void testFetchWithDetach() {
+        long id = createParentAndChildren();
+     
+        EntityManager em = emf.createEntityManager();
+        OpenJPAPersistence.cast(em).setAutoDetach(AutoDetach.DETACH_NONTXREAD);
+        DetachmentOneManyParent parent = em.find(DetachmentOneManyParent.class,
+            id);
+        assertNotNull(parent);
+        assertEquals("parent", parent.getName());
+        assertEquals(2, parent.getChildren().size());
+        DetachmentOneManyChild child0 = parent.getChildren().get(0);
+        DetachmentOneManyChild child1 = parent.getChildren().get(1);
+        assertNotNull("Did not find expected first child", child0);
+        assertNotNull("Did not find expected second child", child1);
+        assertEquals("child0", child0.getName());
+        assertFalse("The first child was not detached", em.contains(child0));
+        assertEquals("child1", child1.getName());
+        assertFalse("The second child was not detached", em.contains(child1));
+        em.close();
+    }
+    
+    public void testFetchWithDetachForToOneRelationship() {
+        long id = createParentAndChildren();
+        
+        EntityManager em = emf.createEntityManager();
+        OpenJPAPersistence.cast(em).setAutoDetach(AutoDetach.DETACH_NONTXREAD);
+        DetachmentOneManyParent parent = em.find(DetachmentOneManyParent.class,
+            id);
+        assertNotNull(parent);
+        assertEquals(2, parent.getChildren().size());
+        assertEquals("ToOne relationship was not eagerly fetched", 
+              parent, parent.getChildren().get(0).getParent());
+        em.close();
+    }
+    
+    private long createParentAndChildren() {
+        DetachmentOneManyParent parent = new DetachmentOneManyParent();
+        parent.setName("parent");
+        for (int i = 0; i < 2; i++) {
+            DetachmentOneManyChild child = new DetachmentOneManyChild();
+            child.setName("child" + i);
+            parent.addChild(child);
+        }
+      
+        EntityManager em = emf.createEntityManager();
+        em.getTransaction().begin();
+        em.persist(parent);
+        em.getTransaction().commit();
+        long id = parent.getId();
+        assertNotNull(parent.getChildren());
+        assertEquals(2, parent.getChildren().size());
+        assertTrue("The parent is not managed", em.contains(parent));
+        DetachmentOneManyChild child0 = parent.getChildren().get(0);
+        DetachmentOneManyChild child1 = parent.getChildren().get(1);
+        assertEquals("child0", child0.getName());
+        assertEquals("child1", child1.getName());
+        assertEquals("The first child has no relationship to the parent", 
+            parent, child0.getParent());
+        assertEquals("The second child has no relationship to the parent", 
+            parent, child1.getParent());
+        em.close();
+        return id;
+    }
+
+    public static void main(String[] args) {
+        TestRunner.run(TestDetachmentOneMany.class);
+    }
+}
+

Propchange: incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detachment/TestDetachmentOneMany.java
------------------------------------------------------------------------------
    svn:executable = *