From 832d913520d909b587cd52ae16e63773965a17ad Mon Sep 17 00:00:00 2001 From: "A. Abram White" Date: Tue, 12 Sep 2006 21:39:01 +0000 Subject: [PATCH] Delete child object(s) before parent when cascading delete. Also fix case where an interface field uses targetEntity to declare itself as a concrete entity relation, and add test case for fix. git-svn-id: https://svn.apache.org/repos/asf/incubator/openjpa/trunk@442718 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/openjpa/kernel/BrokerImpl.java | 2 +- .../persistence/inheritance/EntityL3.java | 15 +++ .../inheritance/MappedSuperclassBase.java | 15 +++ .../inheritance/MappedSuperclassL2.java | 15 +++ .../relations/CascadingOneManyChild.java | 17 +++- .../relations/CascadingOneManyParent.java | 15 +++ .../openjpa/persistence/relations/IFace.java | 21 +++++ .../TargetedIFaceRelationParent.java | 61 ++++++++++++ .../relations/TestTargetedIFaceRelations.java | 93 +++++++++++++++++++ .../AnnotationPersistenceMetaDataParser.java | 4 +- 10 files changed, 254 insertions(+), 4 deletions(-) create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/IFace.java create mode 100755 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/TargetedIFaceRelationParent.java create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/TestTargetedIFaceRelations.java diff --git a/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java b/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java index 51ff98d9c..7500d2f68 100644 --- a/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java +++ b/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java @@ -2404,9 +2404,9 @@ public class BrokerImpl if (sm != null) { if (sm.isDetached()) throw newDetachedException(obj, "delete"); - sm.delete(); if ((action & OpCallbacks.ACT_CASCADE) != 0) sm.cascadeDelete(call); + sm.delete(); } else if (assertPersistenceCapable(obj).pcIsDetached() == Boolean.TRUE) throw newDetachedException(obj, "delete"); } diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/EntityL3.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/EntityL3.java index 8e896e4e5..9b056ef50 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/EntityL3.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/EntityL3.java @@ -1,3 +1,18 @@ +/* + * 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.inheritance; import javax.persistence.Entity; diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/MappedSuperclassBase.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/MappedSuperclassBase.java index af9c6786c..06f9039cc 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/MappedSuperclassBase.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/MappedSuperclassBase.java @@ -1,3 +1,18 @@ +/* + * 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.inheritance; import javax.persistence.GeneratedValue; diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/MappedSuperclassL2.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/MappedSuperclassL2.java index de102d2e6..a64679350 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/MappedSuperclassL2.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/MappedSuperclassL2.java @@ -1,3 +1,18 @@ +/* + * 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.inheritance; import javax.persistence.MappedSuperclass; diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/CascadingOneManyChild.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/CascadingOneManyChild.java index 7978664b9..5614018a6 100755 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/CascadingOneManyChild.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/CascadingOneManyChild.java @@ -1,3 +1,18 @@ +/* + * 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.relations; import javax.persistence.Column; @@ -22,7 +37,7 @@ public class CascadingOneManyChild { private String name; @ManyToOne(optional=false) - @JoinColumn(name="PARENT_ID") + @JoinColumn(name="PARENT_ID", nullable=false) @ForeignKey private CascadingOneManyParent parent; diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/CascadingOneManyParent.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/CascadingOneManyParent.java index fe7fd5eb9..78f201b06 100755 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/CascadingOneManyParent.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/CascadingOneManyParent.java @@ -1,3 +1,18 @@ +/* + * 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.relations; import java.util.ArrayList; diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/IFace.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/IFace.java new file mode 100644 index 000000000..940fea5cc --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/IFace.java @@ -0,0 +1,21 @@ +/* + * 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.relations; + +public interface IFace { + + public String getName(); +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/TargetedIFaceRelationParent.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/TargetedIFaceRelationParent.java new file mode 100755 index 000000000..88bc8ff0a --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/TargetedIFaceRelationParent.java @@ -0,0 +1,61 @@ +/* + * 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.relations; + +import javax.persistence.CascadeType; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.ManyToOne; +import javax.persistence.Version; + +@Entity +public class TargetedIFaceRelationParent + implements IFace { + + @Id + @GeneratedValue + private long id; + + private String name; + + @ManyToOne(targetEntity=TargetedIFaceRelationParent.class, + cascade=CascadeType.ALL) + private IFace iface; + + @Version + private Integer optLock; + + public long getId() { + return id; + } + + public IFace getIFace() { + return iface; + } + + public void setIFace(IFace iface) { + this.iface = iface; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/TestTargetedIFaceRelations.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/TestTargetedIFaceRelations.java new file mode 100644 index 000000000..564227a71 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/TestTargetedIFaceRelations.java @@ -0,0 +1,93 @@ +/* + * 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.relations; + +import java.util.HashMap; +import java.util.Map; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.EntityTransaction; +import javax.persistence.Persistence; + +import junit.framework.TestCase; +import junit.textui.TestRunner; + +/** + * Perform basic operations on an entity with interface relations that use + * the targetEntity attribute to set a concrete related type. + * + * @author Abe White + */ +public class TestTargetedIFaceRelations + extends TestCase { + + private EntityManagerFactory emf; + + public void setUp() { + Map props = new HashMap(); + props.put("openjpa.MetaDataFactory", "jpa(Types=" + + TargetedIFaceRelationParent.class.getName() + ")"); + emf = Persistence.createEntityManagerFactory("test", props); + } + + public void tearDown() { + if (emf == null) + return; + try { + EntityManager em = emf.createEntityManager(); + em.getTransaction().begin(); + em.createQuery("delete from TargetedIFaceRelationParent"). + executeUpdate(); + em.getTransaction().commit(); + em.close(); + emf.close(); + } catch (Exception e) { + } + } + + public void testPersist() { + TargetedIFaceRelationParent parent = new TargetedIFaceRelationParent(); + parent.setName("parent"); + TargetedIFaceRelationParent child = new TargetedIFaceRelationParent(); + child.setName("child"); + parent.setIFace(child); + + EntityManager em = emf.createEntityManager(); + em.getTransaction().begin(); + em.persist(parent); + em.getTransaction().commit(); + long id = parent.getId(); + assertTrue(id != 0); + em.close(); + + em = emf.createEntityManager(); + parent = em.find(TargetedIFaceRelationParent.class, id); + assertNotNull(parent); + assertEquals("parent", parent.getName()); + assertNotNull(parent.getIFace()); + assertEquals("child", parent.getIFace().getName()); + assertEquals(TargetedIFaceRelationParent.class, + parent.getIFace().getClass()); + assertNull(((TargetedIFaceRelationParent) parent.getIFace()). + getIFace()); + em.close(); + } + + public static void main(String[] args) { + TestRunner.run(TestTargetedIFaceRelations.class); + } +} + diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/AnnotationPersistenceMetaDataParser.java b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/AnnotationPersistenceMetaDataParser.java index 67131270b..8b70525e5 100644 --- a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/AnnotationPersistenceMetaDataParser.java +++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/AnnotationPersistenceMetaDataParser.java @@ -1200,7 +1200,7 @@ public class AnnotationPersistenceMetaDataParser if (!anno.optional()) fmd.setNullValue(FieldMetaData.NULL_EXCEPTION); if (anno.targetEntity() != void.class) - fmd.setDeclaredType(anno.targetEntity()); + fmd.setTypeOverride(anno.targetEntity()); setCascades(fmd, anno.cascade()); } @@ -1223,7 +1223,7 @@ public class AnnotationPersistenceMetaDataParser if (isMappingOverrideMode() && !StringUtils.isEmpty(anno.mappedBy())) fmd.setMappedBy(anno.mappedBy()); if (anno.targetEntity() != void.class) - fmd.setDeclaredType(anno.targetEntity()); + fmd.setTypeOverride(anno.targetEntity()); setCascades(fmd, anno.cascade()); }