users@glassfish.java.net

Cascade delete problem

From: <glassfish_at_javadesktop.org>
Date: Thu, 05 Feb 2009 04:44:01 PST

Hi,

I have a problem with cascading delete. I have two enteties, A and B. One instance of A can have several instances of B in a Collection<B>. The two enteties are joined with a join table that have two columns, the PK from the table of A and B. B is not aware of the relation. This is the source code:

@Entity
@Table(name="a")
public class A implements Serializable
{
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name = "id", table = "a")
        private int id;
        
        @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
        @JoinTable(name = "a_b",
                        joinColumns = {_at_JoinColumn(name = "a_fk")},
                        inverseJoinColumns = {_at_JoinColumn(name = "b_fk")})
        private Collection<B> b;
        ...
}

@Entity
@Table(name="b")
public class B implements Serializable
{
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name = "id", table = "b")
        private int id;
        ...
}

When I delete an instance of A(which have a list of B), with EntityManager.remove(A), the correct row is deleted in table a and the corresponding rows are deleted from the join table a_b. BUT the rows in table b, that was belonging to the deleted instance of A are NOT removed. How can this be? What have I done wrong? Or is this a problem with Toplink essentials?
[Message sent by forum member 'nightzero' (nightzero)]

http://forums.java.net/jive/thread.jspa?messageID=330288