Whilch OpenJPA version are you using?
Verified that cascade delete on bi-directional relationship works with
current OpenJPA.
May be you should post a isolated test case to demonstrate the problem for
analysis.
Here is a test case that works.
A Owner-Child bi-directional relationship where deletion of Owner deletes
all Child but deletion of Child does not delete Owner.
==========================================
@Entity
public class Owner {
@Id @GeneratedValue
private long id;
@OneToMany(mappedBy="owner", cascade=CascadeType.ALL)
private Set<Child> children;
============================================
@Entity
public class Child {
@Id @GeneratedValue
private long id;
@ManyToOne(cascade = {PERSIST,REFRESH,MERGE})
private Owner owner;
============================================
Under this class definitions, following test cases that deletes owner and
deletes child do pass:
==============================================
package delete;
import java.util.Iterator;
import java.util.Set;
import javax.persistence.*;
import junit.framework.TestCase;
/**
* Demonstrates cascade delete operation on bi-directional Owner-Child
relation.
* Owner has @OneToMany relationship to Child. Child has @ManyToOne
relationship
* to Owner.
* Owner cascade deletes all its children. Deletion of Child does not delete
its
* Owner.
*
* @author Pinaki Poddar
*
*/
public class TestDelete extends TestCase {
private static EntityManagerFactory emf;
protected void setUp() throws Exception {
super.setUp();
if (emf == null)
emf = Persistence.createEntityManagerFactory("delete");
}
/**
* Persist a new Owner and few children.
* Verify that they are persisted by counting total numbers.
* Delete the owner. All children must get deleted by cascade.
* Verify deletion by counting total numbers.
*
*/
public void testDeleteOwnerCascadeDeletesAllChildren() {
int nOwnerBefore = count(Owner.class);
int nChildBefore = count(Child.class);
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
int N = 4;
Owner owner = new Owner();
for (int i=0; i<N; i++)
owner.addChild(new Child());
em.persist(owner);
em.getTransaction().commit();
assertEquals(nOwnerBefore+1, count(Owner.class));
assertEquals(nChildBefore+N, count(Child.class));
em.getTransaction().begin();
em.remove(owner);
em.getTransaction().commit();
assertEquals(nOwnerBefore, count(Owner.class));
assertEquals(nChildBefore, count(Child.class));
}
public void testDeleteChildDoesNotDeleteOwner() {
int nOwnerBefore = count(Owner.class);
int nChildBefore = count(Child.class);
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
int N = 4;
Owner owner = new Owner();
for (int i=0; i<N; i++)
owner.addChild(new Child());
em.persist(owner);
em.getTransaction().commit();
assertEquals(nOwnerBefore+1, count(Owner.class));
assertEquals(nChildBefore+N, count(Child.class));
em.getTransaction().begin();
Set<Child> children = owner.getChildren();
assertEquals(N, children.size());
Iterator<Child> iter = children.iterator();
em.remove(iter.next());
em.getTransaction().commit();
assertEquals(nOwnerBefore+1, count(Owner.class));
assertEquals(nChildBefore+N-1, count(Child.class));
assertEquals(N-1, owner.getChildren().size());
}
/**
* Get total number of instances of the given class
*/
int count(Class c) {
return count(c.getSimpleName());
}
int count(String alias) {
EntityManager em = emf.createEntityManager();
Object count = em.createQuery("SELECT COUNT(p) FROM " +
alias + " p").getSingleResult();
return ((Long)count).intValue();
}
}
==============================================
--
View this message in context:
http://www.nabble.com/Cascade-on-Delete-is-not-working-tp14529474p14765995.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.