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
This commit is contained in:
A. Abram White 2006-09-12 21:39:01 +00:00
parent 6ac482c68f
commit 832d913520
10 changed files with 254 additions and 4 deletions

View File

@ -2404,9 +2404,9 @@ public class BrokerImpl
if (sm != null) { if (sm != null) {
if (sm.isDetached()) if (sm.isDetached())
throw newDetachedException(obj, "delete"); throw newDetachedException(obj, "delete");
sm.delete();
if ((action & OpCallbacks.ACT_CASCADE) != 0) if ((action & OpCallbacks.ACT_CASCADE) != 0)
sm.cascadeDelete(call); sm.cascadeDelete(call);
sm.delete();
} else if (assertPersistenceCapable(obj).pcIsDetached() == Boolean.TRUE) } else if (assertPersistenceCapable(obj).pcIsDetached() == Boolean.TRUE)
throw newDetachedException(obj, "delete"); throw newDetachedException(obj, "delete");
} }

View File

@ -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; package org.apache.openjpa.persistence.inheritance;
import javax.persistence.Entity; import javax.persistence.Entity;

View File

@ -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; package org.apache.openjpa.persistence.inheritance;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;

View File

@ -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; package org.apache.openjpa.persistence.inheritance;
import javax.persistence.MappedSuperclass; import javax.persistence.MappedSuperclass;

View File

@ -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; package org.apache.openjpa.persistence.relations;
import javax.persistence.Column; import javax.persistence.Column;
@ -22,7 +37,7 @@ public class CascadingOneManyChild {
private String name; private String name;
@ManyToOne(optional=false) @ManyToOne(optional=false)
@JoinColumn(name="PARENT_ID") @JoinColumn(name="PARENT_ID", nullable=false)
@ForeignKey @ForeignKey
private CascadingOneManyParent parent; private CascadingOneManyParent parent;

View File

@ -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; package org.apache.openjpa.persistence.relations;
import java.util.ArrayList; import java.util.ArrayList;

View File

@ -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();
}

View File

@ -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;
}
}

View File

@ -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);
}
}

View File

@ -1200,7 +1200,7 @@ public class AnnotationPersistenceMetaDataParser
if (!anno.optional()) if (!anno.optional())
fmd.setNullValue(FieldMetaData.NULL_EXCEPTION); fmd.setNullValue(FieldMetaData.NULL_EXCEPTION);
if (anno.targetEntity() != void.class) if (anno.targetEntity() != void.class)
fmd.setDeclaredType(anno.targetEntity()); fmd.setTypeOverride(anno.targetEntity());
setCascades(fmd, anno.cascade()); setCascades(fmd, anno.cascade());
} }
@ -1223,7 +1223,7 @@ public class AnnotationPersistenceMetaDataParser
if (isMappingOverrideMode() && !StringUtils.isEmpty(anno.mappedBy())) if (isMappingOverrideMode() && !StringUtils.isEmpty(anno.mappedBy()))
fmd.setMappedBy(anno.mappedBy()); fmd.setMappedBy(anno.mappedBy());
if (anno.targetEntity() != void.class) if (anno.targetEntity() != void.class)
fmd.setDeclaredType(anno.targetEntity()); fmd.setTypeOverride(anno.targetEntity());
setCascades(fmd, anno.cascade()); setCascades(fmd, anno.cascade());
} }