This content has been marked as final.
Show 3 replies
-
1. Re: Deleting an entity from another entity?
jacekolszak Dec 13, 2005 4:33 AM (in response to rbenko)Hmm... maybe you should addnotate the collection with Cascade.REMOVE ?
For example:public class Container { private List<Item> items; @OneToMany(cascade=CascadeType.REMOVE) public List<Item> getItems(); }
When you remove an item from the list, you removed the entity from the datastore too.getItems().remove(0); em.merge(container);
I think it should work, but not sure for 100%. -
2. Re: Deleting an entity from another entity?
rbenko Dec 13, 2005 10:51 AM (in response to rbenko)Thanks for the reply. Collection is already annotated with Cascade.REMOVE - merge() doesn't work in this instance. I think I need a way to get to the EntityManager inside my Entity Bean. For example:
@Entity public class Container { private List<Item> items; @OneToMany(mappedBy="security", cascade=CascadeType.ALL) @MapKey (name="key") public Map<String, SecProperty>getProperties(); ... public void removeProperty(String key) { if (getProperties().containsKey(key)) { Object obj = getMyProperties().remove(key); // // This is where I think I could use the EntityManager // to delete from database: // em.remove(obj); } } }
What is the best way to inject the EntityManager into an Entity Bean? Or is there is another way to accomplish this without using the EntityManager? -
3. Re: Deleting an entity from another entity?
rbenko Dec 13, 2005 3:21 PM (in response to rbenko)Okay - more searching led to a 'workaround' using Hibernate's native CascadeTyp.DELETE_ORPHAN setting. I'm not quite sure why a facility like this or like TopLink's 'Private Owned' flag is left out of the EJB spec - it seems like a fairly common mechanism.