[jboss-user] [EJB 3.0] - Re: Working example por OneToMany cascade remove?
waynebaylor
do-not-reply at jboss.com
Fri Aug 10 18:29:26 EDT 2007
here's an example i just tested:
| @Entity
| public class Parent
| {
| @Id
| @GeneratedValue
| private Long id;
|
| @OneToMany(mappedBy="parent", cascade=CascadeType.REMOVE)
| private Set<Child> children = new HashSet<Child>();
|
| public Set<Child> getChildren()
| {
| return children;
| }
|
| public void setChildren(Set<Child> children)
| {
| this.children=children;
| }
|
| public Long getId()
| {
| return id;
| }
| }
| ==========================
|
|
| @Entity
| public class Child
| {
| @Id
| @GeneratedValue
| private Long id;
|
| @ManyToOne
| private Parent parent;
|
| private String name;
|
| public Child(){}
|
| public Child(String name)
| {
| this.name = name;
| }
| ...
| }
|
and here is the code that creates and removes the entities:
@Stateless
| @Local(TesterLocal.class)
| @LocalBinding(jndiBinding="session.Tester/local")
| public class Tester implements TesterLocal
| {
| @PersistenceContext(unitName="PU2")
| private EntityManager em;
|
| public void doWork()
| {
| Parent p = new Parent();
| em.persist(p);
|
| for(int i=0; i<5; ++i)
| {
| Child c = new Child("child-"+i);
| c.setParent(p);
| p.getChildren().add(c);
|
| em.persist(c);
| }
| em.flush();
| p = null;
|
| Parent pp = em.find(Parent.class, 1L); //i know the id is 1
| em.remove(pp);
| em.flush();
| }
| }
this code does not throw any exceptions. checking the DB i can see that the parent and all children are removed.
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4073219#4073219
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4073219
More information about the jboss-user
mailing list