openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Alexander Saint Croix" <saintx.opensou...@gmail.com>
Subject Re: Cascade on Delete is not working
Date Fri, 11 Jan 2008 23:31:53 GMT
Pinaki,

Thanks for your example.  Convinced that I might be doing something wrong I
scraped the test method and rewrote it from scratch, changing nothing in the
entities.  Now it works as expected.

I'll have to figure out why that happened.  Perhaps when I set up the
entities I incorrectly wired them together, not correctly setting the
fields, for example.

This is great--I'd consigned myself to doing things the hard way, but I'm
very pleased (if not surprised) by OpenJPA's strengths in this arena.
Thanks again for the help (and please forgive the scrawls).

Cheers,
--
Alexander Saint Croix



On Jan 11, 2008 3:49 PM, Pinaki Poddar <ppoddar@apache.org> wrote:

>
> 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.
>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message