3 Replies Latest reply on Dec 13, 2005 3:21 PM by rbenko

    Deleting an entity from another entity?

      This might be a stupid question, but I can't seem to find the answer to it. I'm fairly new to JBoss/Hibernate/EJB3.0, but I have a good deal of experience using TopLink. I have a collection of entity objects contained in another entity object. When I remove an entity from the collection (via a method on the owning entity) I would like to remove it from the datastore - how do I go about it? Do I need to get a handle to the EntityManager and call remove()? If so, what is the best way to get a handle to the EntityManager inside an Entity Bean?

      TopLink has a setting called 'Private Owned', which would automatically delete an object if you removed it from the owning entity's collection. I see that Hibernate does this for simple collections (Strings and the like), but not for Entity collections. Thanks for any help.

        • 1. Re: Deleting an entity from another entity?
          jacekolszak

          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?

            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?

              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.