You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by jr...@apache.org on 2009/03/25 22:44:00 UTC

svn commit: r758442 - in /openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detach: EntityA.java EntityB.java EntityC.java EntityD.java EntityE.java TestDetachStateCascade.java

Author: jrbauer
Date: Wed Mar 25 21:43:57 2009
New Revision: 758442

URL: http://svn.apache.org/viewvc?rev=758442&view=rev
Log:
OPENJPA-885 Committing additional detach tests contributed by Dianne Richards

Added:
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detach/EntityA.java   (with props)
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detach/EntityB.java   (with props)
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detach/EntityC.java   (with props)
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detach/EntityD.java   (with props)
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detach/EntityE.java   (with props)
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detach/TestDetachStateCascade.java   (with props)

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detach/EntityA.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detach/EntityA.java?rev=758442&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detach/EntityA.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detach/EntityA.java Wed Mar 25 21:43:57 2009
@@ -0,0 +1,156 @@
+/*
+ * 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.detach;
+
+import javax.persistence.Basic;
+import javax.persistence.CascadeType;
+import javax.persistence.Entity;
+import javax.persistence.FetchType;
+import javax.persistence.Id;
+import javax.persistence.OneToOne;
+import javax.persistence.Table;
+
+import org.apache.openjpa.persistence.FetchAttribute;
+import org.apache.openjpa.persistence.FetchGroup;
+import org.apache.openjpa.persistence.FetchGroups;
+
+@Entity
+@Table(name="EntityA_detach")
+@FetchGroups({
+    @FetchGroup(name = "loadD", attributes = {
+    @FetchAttribute(name = "entityD", recursionDepth = 0)
+        })
+        })
+public class EntityA {
+    @Id
+    private long id;
+    
+    private String name;
+    
+    @Basic(fetch=FetchType.LAZY)
+    private String description;
+    
+    @OneToOne(cascade=(CascadeType.ALL))
+    EntityB entityB;
+    
+    @OneToOne(cascade=(CascadeType.PERSIST))
+    EntityC entityC;
+    
+    @OneToOne(cascade=(CascadeType.ALL), fetch=FetchType.LAZY)
+    EntityD entityD;
+    
+    @OneToOne(cascade=(CascadeType.PERSIST), fetch=FetchType.LAZY)
+    EntityE entityE;
+    
+    public EntityA() {
+    }
+    
+    public EntityA(long id, String name, String description) {
+        this.id = id;
+        this.name = name;
+        this.description = description;
+    }
+    
+    public long getId() {
+        return id;
+    }
+
+    public void setId(long id) {
+        this.id = id;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    /**
+     * @return the description
+     */
+    public String getDescription() {
+        return description;
+    }
+
+    /**
+     * @param description the description to set
+     */
+    public void setDescription(String description) {
+        this.description = description;
+    }
+
+    /**
+     * @return the entityB
+     */
+    public EntityB getEntityB() {
+        return entityB;
+    }
+
+    /**
+     * @param entityB the entityB to set
+     */
+    public void setEntityB(EntityB entityB) {
+        this.entityB = entityB;
+    }
+
+    /**
+     * @return the entityC
+     */
+    public EntityC getEntityC() {
+        return entityC;
+    }
+
+    /**
+     * @param entityC the entityC to set
+     */
+    public void setEntityC(EntityC entityC) {
+        this.entityC = entityC;
+    }
+
+    /**
+     * @return the entityD
+     */
+    public EntityD getEntityD() {
+        return entityD;
+    }
+
+    /**
+     * @param entityD the entityD to set
+     */
+    public void setEntityD(EntityD entityD) {
+        this.entityD = entityD;
+    }
+
+    /**
+     * @return the entityE
+     */
+    public EntityE getEntityE() {
+        return entityE;
+    }
+
+    /**
+     * @param entityE the entityE to set
+     */
+    public void setEntityE(EntityE entityE) {
+        this.entityE = entityE;
+    }
+
+}

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

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detach/EntityB.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detach/EntityB.java?rev=758442&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detach/EntityB.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detach/EntityB.java Wed Mar 25 21:43:57 2009
@@ -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.detach;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.Table;
+
+@Entity
+@Table(name="EntityB_detach")
+public class EntityB {
+    @Id
+    private long id;
+    
+    private String name;
+
+    public EntityB() {
+    }
+    
+    public EntityB(long id, String name) {
+        this.id = id;
+        this.name = name;
+    }
+    
+    public long getId() {
+        return id;
+    }
+
+    public void setId(long id) {
+        this.id = id;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+}

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

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detach/EntityC.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detach/EntityC.java?rev=758442&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detach/EntityC.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detach/EntityC.java Wed Mar 25 21:43:57 2009
@@ -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.detach;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.Table;
+
+@Entity
+@Table(name="EntityC_detach")
+public class EntityC {
+    @Id
+    private long id;
+    
+    private String name;
+
+    public EntityC() {
+    }
+    
+    public EntityC(long id, String name) {
+        this.id = id;
+        this.name = name;
+    }
+    
+    public long getId() {
+        return id;
+    }
+
+    public void setId(long id) {
+        this.id = id;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+}

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

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detach/EntityD.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detach/EntityD.java?rev=758442&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detach/EntityD.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detach/EntityD.java Wed Mar 25 21:43:57 2009
@@ -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.detach;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.Table;
+
+@Entity
+@Table(name="EntityD_detach")
+public class EntityD {
+    @Id
+    private long id;
+    
+    private String name;
+
+    public EntityD() {
+    }
+    
+    public EntityD(long id, String name) {
+        this.id = id;
+        this.name = name;
+    }
+    
+    public long getId() {
+        return id;
+    }
+
+    public void setId(long id) {
+        this.id = id;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+}

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

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detach/EntityE.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detach/EntityE.java?rev=758442&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detach/EntityE.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detach/EntityE.java Wed Mar 25 21:43:57 2009
@@ -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.detach;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.Table;
+
+@Entity
+@Table(name="EntityE_detach")
+public class EntityE {
+    @Id
+    private long id;
+    
+    private String name;
+
+    public EntityE() {
+    }
+    
+    public EntityE(long id, String name) {
+        this.id = id;
+        this.name = name;
+    }
+    
+    public long getId() {
+        return id;
+    }
+
+    public void setId(long id) {
+        this.id = id;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+}

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

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detach/TestDetachStateCascade.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detach/TestDetachStateCascade.java?rev=758442&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detach/TestDetachStateCascade.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detach/TestDetachStateCascade.java Wed Mar 25 21:43:57 2009
@@ -0,0 +1,189 @@
+/*
+ * 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.detach;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
+
+import org.apache.openjpa.conf.Compatibility;
+import org.apache.openjpa.persistence.DetachStateType;
+import org.apache.openjpa.persistence.OpenJPAEntityManager;
+import org.apache.openjpa.persistence.test.SingleEMFTestCase;
+
+/**
+ * Test the various options for openjpa.DetachState with the new DETACH
+ * cascade option.
+ * 
+ * @author dianner
+ * @since 2.0.0
+ *
+ */
+public class TestDetachStateCascade extends SingleEMFTestCase {
+    OpenJPAEntityManager em;
+    int id = 0;
+    Compatibility compat;
+    
+    EntityA entityA;
+    EntityB entityB;
+    EntityC entityC;
+    EntityD entityD;
+    EntityE entityE;
+    
+    List<EntityC> list = new ArrayList<EntityC>();
+    
+    Collection<Object> allEntities = new HashSet<Object>();
+    
+    public void setUp() throws Exception {
+        setUp(org.apache.openjpa.persistence.detach.EntityA.class,
+            org.apache.openjpa.persistence.detach.EntityB.class,
+            org.apache.openjpa.persistence.detach.EntityC.class,
+            org.apache.openjpa.persistence.detach.EntityD.class,
+            org.apache.openjpa.persistence.detach.EntityE.class);
+        assertNotNull(emf);
+        em = emf.createEntityManager();
+        assertNotNull(em);
+        compat = emf.getConfiguration().getCompatibilityInstance();
+        assertNotNull(compat);
+        id++;
+        create(id);
+        
+        em.getTransaction().begin();
+        em.persist(entityA);
+        em.getTransaction().commit();
+    }
+
+    private void create(int id) {
+        entityA = new EntityA(id, "entityA", "top level class");
+        entityB = new EntityB(id, "entityB");
+        entityC = new EntityC(id, "entityC");
+        entityD = new EntityD(id, "entityD");
+        entityE = new EntityE(id, "entityE");
+        
+        entityA.setEntityB(entityB);
+        entityA.setEntityC(entityC);
+        entityA.setEntityD(entityD);
+        entityA.setEntityE(entityE);
+    }
+    
+    
+    
+    /**
+     * The default DetachState of LOADED is tested. In this scenario:
+     * A is the main entity to be detached
+     * B is loaded and DETACH cascade is specified
+     * C is loaded but DETACH cascade is NOT specified
+     * D is not loaded and DETACH cascade is specified
+     * E is not loaded but DETACH cascade is NOT specified
+     */
+    public void testLoaded() {
+        em.getTransaction().begin();
+        // Clear the persistence context so we start fresh
+        em.persist(entityA);
+        em.clear();
+        EntityA eA = em.find(EntityA.class, id); // Loads B and C by default
+        assertTrue(em.contains(eA));
+        EntityD eD = em.find(EntityD.class, id); // Load independently
+        assertTrue(em.contains(eD));
+        EntityE eE = em.find(EntityE.class, id); // Load independently
+        assertTrue(em.contains(eE));
+        EntityA entityADet = em.detach(eA);
+        assertEquals(id, entityADet.getId());
+        assertEquals("entityA", entityADet.getName());
+        assertNull(entityADet.getDescription()); // should not be loaded
+        assertNotNull(entityADet.getEntityB());
+        assertNotNull(entityADet.getEntityC());
+        assertNull(entityADet.getEntityD());
+        assertNull(entityADet.getEntityE());
+        assertTrue(em.isDetached(eA));
+        assertTrue(em.isDetached(entityADet.getEntityB()));
+        assertFalse(em.isDetached(entityADet.getEntityC()));
+        assertFalse(em.isDetached(eD));
+        assertFalse(em.isDetached(eE));
+        em.getTransaction().commit();
+    }
+    
+    /**
+     * The DetachState of ALL is tested. In this scenario:
+     * A is the main entity to be detached.
+     * B, C, D, and E are all loaded
+     * DETACH cascade is specified for B and D
+     * DETACH cascade is NOT specified for C and E
+     */
+    public void testDetachStateAll() {
+        em.setDetachState(DetachStateType.ALL);
+        em.getTransaction().begin();
+        // Clear the persistence context so we start fresh
+        em.persist(entityA);
+        em.clear();
+        EntityA eA = em.find(EntityA.class, id);
+        assertTrue(em.contains(eA));
+        EntityA entityADet = em.detach(eA);
+        assertEquals(id, entityADet.getId());
+        assertEquals("entityA", entityADet.getName());
+        assertNotNull(entityADet.getDescription());
+        assertNotNull(entityADet.getEntityB());
+        assertNotNull(entityADet.getEntityC());
+        assertNotNull(entityADet.getEntityD());
+        assertNotNull(entityADet.getEntityE());
+        assertTrue(em.isDetached(eA));
+        assertTrue(em.isDetached(entityADet.getEntityB()));
+        assertFalse(em.isDetached(entityADet.getEntityC()));
+        assertTrue(em.isDetached(entityADet.getEntityD()));
+        assertFalse(em.isDetached(entityADet.getEntityE()));
+        em.getTransaction().commit();
+    }
+    
+    /**
+     * The default DetachState of LOADED is tested. In this scenario:
+     * A is the main entity to be detached
+     * B is loaded by default and DETACH cascade is specified
+     * C is loaded by default but DETACH cascade is NOT specified
+     * D is loaded by fetch plan and DETACH cascade is specified
+     * E is not loaded but DETACH cascade is NOT specified
+     */
+    public void testDetachStateFetchGroup() {
+        em.setDetachState(DetachStateType.FETCH_GROUPS);
+        em.getFetchPlan().addFetchGroup("loadD");
+        em.getTransaction().begin();
+        // Clear the persistence context so we start fresh
+        em.persist(entityA);
+        em.clear();
+        EntityA eA = em.find(EntityA.class, id);
+        assertTrue(em.contains(eA));
+        EntityE eE = em.find(EntityE.class, id); // Load independently
+        assertTrue(em.contains(eE));
+        EntityA entityADet = em.detach(eA);
+        assertEquals(id, entityADet.getId());
+        assertEquals("entityA", entityADet.getName());
+        assertNull(entityADet.getDescription());
+        assertNotNull(entityADet.getEntityB());
+        assertNotNull(entityADet.getEntityC());
+        assertNotNull(entityADet.getEntityD());
+        assertNull(entityADet.getEntityE());
+        assertTrue(em.isDetached(eA));
+        assertTrue(em.isDetached(entityADet.getEntityB()));
+        assertFalse(em.isDetached(entityADet.getEntityC()));
+        assertTrue(em.isDetached(entityADet.getEntityD()));
+        assertFalse(em.isDetached(eE));
+        em.getTransaction().commit();
+    }
+    
+}

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