Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » JBoss User Help »

[jboss-user] [EJB 3.0] - Re: Working example por OneToMany cascade
 remove?

scott.stark@jboss.org

2007-08-10


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
_______________________________________________
jboss-user mailing list
jboss-user@(protected)
https://lists.jboss.org/mailman/listinfo/jboss-user
©2008 junlu.com - Jax Systems, LLC, U.S.A.