5 Replies Latest reply on Sep 10, 2006 11:35 AM by aberezin

    deleting entities from a Collection

    aberezin

      Sorry if this is a bit of a repost. I am trying to figure out the best way to remove enties from a OneToMany collection (either uni or bidirectional).

      I have an entity Foo, that contains a OneToMany Set:
      Set getBars()

      If I add new Bar objects to the set and call em.merge(myFoo), it properly persists the new Bar objects. However, simply removing Bar objects from the set and calling em.merge(myFoo) does not remove the underlying Bar objects from the db. It seems I have to explicitly call em.remove(myBar1), ...

      Is this how thinks are suppose to work? I could see in a ManyToMany that you could not delete the underlying row simply because it is no longer in the Set. However, in a OneToMany, it would seem that the any Bar objects no longer in the set should be deleted.

        • 1. Re: deleting entities from a Collection
          aberezin

          I think I have part of the answer: See http://www.jboss.com/index.html?module=bb&op=viewtopic&t=56357

          The short answer seem to be that in a bidirectional OneToMany, you must explicitly delete the entity after removing it from the collection. However, in that post, Bill Burke seems to indicate that a "non-inverse", which I assume is a unidirectional relationship, would automatically delete the entity when it is removed from the collection.

          • 2. Re: deleting entities from a Collection
            a_titov82

            And is there any way to remove related object without explicitly calling EntityManager.remove and EntityManager.find? I have no EntityManager instance in remote client application... Maybe I can just remove related entity from collection in base object and then save base object somehow?

            • 3. Re: deleting entities from a Collection
              laszlo.fogas

              define a hibernate specific annotation, because this kind of deleting ejb3 spec does not support. In hibernate it's delete_orphan cascade type..

              use

              @OneToMany(fetch = FetchType.LAZY, mappedBy = "felhasznalok")
               @org.hibernate.annotations.Cascade( {
               org.hibernate.annotations.CascadeType.ALL,
               org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
               public Set<Kedvencek> getKedvenceks() {
               return this.kedvenceks;
               }
              
              


              • 4. Re: deleting entities from a Collection
                aberezin

                Thanks for the tip.

                I could seem to get CascadeType.DELETE_ORPHAN to work in 4.04ga with a Set (didnt try with a List). I saw a bug related to this, http://opensource.atlassian.com/projects/hibernate/browse/EJB-53 but it appears fixed long ago.

                I made a little test case that persists the Order with one OrderItem. I then call order.getOrderItems().clear() and then call (thru the sessionbean) em.merge(order) and even do an explicit flush(). The underlying row for the OrderItem is not deleted.

                This whole thing is not a big deal since I can explicitly delete the child objects no longer in the Set. I was just curious as to how to do it automatically.

                • 5. Re: deleting entities from a Collection
                  aberezin

                  Oops, I meant to say that I could NOT get the DELETE_ORPHAN to work.