4 Replies Latest reply on Nov 4, 2005 5:27 AM by epbernard

    EntityManger.merge() and Cascade.Remove

    hfarid

      Hi,
      I am having a problem with cascade delete on "OneToMangy" relation

      I have two entities "Tag" and "EquivalentTag" with oneToMany relation from the "tag" side

      Tag code

      @Entity()
      @Table(name="tag")
      public class Tag implements Serializable{
      
       private int id;
       private String name;
       private String description;
       private Set<EquivalentTag> equivalentTags;
      
       @Id(generate = GeneratorType.AUTO)
       @Column(name = "id")
       public int getId() {
       return id;
       }
      ...
       @OneToMany(cascade = CascadeType.ALL, mappedBy="tag", fetch=FetchType.EAGER )
       public Set<EquivalentTag> getEquivalentTags() {
       return equivalentTags;
       }
      
       public void setEquivalentTags(Set<EquivalentTag> equivalentTags) {
       this.equivalentTags = equivalentTags;
       }
      
      }
      


      EquivalentTag code
      @Entity()
      @Table(name="equivalenttag")
      public class EquivalentTag implements Serializable{
      
       private int id;
       private int eqid;
       private String eqname;
       private Tag tag;
      
      
       @Id(generate = GeneratorType.AUTO)
       @Column(name = "id")
       public int getId() {
       return id;
       }
      ...
       @ManyToOne
       @JoinColumn(name="tag")
       public Tag getTag() {
       return tag;
       }
      
       public void setTag(Tag tag) {
       this.tag = tag;
       }
      
      }
      
      


      My client add/remove EquivalentTags to a tag object... pass it on to the session bean which in turns calls EntityManager.merge(tag)

      in case of adding new EquivalentTag ... eman.merge() is creating all the new objects in the equivalenttag table
      But in case of removing EquivalentTag ... eman.merge() basically does not do anything

      Did I miss anything in my code ?? do I have the wrong understanding of Cascade.Remove (please correct me if this is true). Do I have to do the delete manually ?

      Thanks

      My setup
      - PC (windows XP Pro)
      - Release ID: JBoss [Zion] 4.0.3RC2 (build: CVSTag=Branch_4_0 date=200508131404)
      - jboss-EJB-3.0_RC3

        • 1. Re: EntityManger.merge() and Cascade.Remove
          alximik

          merge won't delete such objects. You can try hibernate's org.hibernate.annotations.CascadeType.DELETE_ORPHAN annotation.

          But I'm thinking its a kludge. Better delete the objects yourself in setEquivalentTags method.

          @OneToMany(cascade = CascadeType.ALL, mappedBy="tag", fetch=FetchType.EAGER )
          @org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.ALL, org.hibernate.annotations.CascadeType.DELETE_ORPHAN } )
          public Set<EquivalentTag> getEquivalentTags()
          .....
          


          • 2. Re: EntityManger.merge() and Cascade.Remove
            hfarid

            works very nicely for a kludge :-)
            I am kinda surprised that merge does not delete the many side of the relation since it does create/update when needed

            anyway, thanks for the hint.

            • 3. Re: EntityManger.merge() and Cascade.Remove
              tonylai

              To epbernard:
              Yes, DELETE_ORPHAN can resolve the problem, but it's really not graceful and untransplantable. The entity bean is tightly bounded with hibernate persist. For hibernate 2, there's no DELETE_ORPHAN. And we are all confused why you guys import such a unusable feature?

              • 4. Re: EntityManger.merge() and Cascade.Remove
                epbernard

                 

                "tonylai" wrote:
                To epbernard:
                Yes, DELETE_ORPHAN can resolve the problem, but it's really not graceful and untransplantable. The entity bean is tightly bounded with hibernate persist. For hibernate 2, there's no DELETE_ORPHAN. And we are all confused why you guys import such a unusable feature?


                Stop claiming false statements. It's not my fault if the EJB3 EG has decided not to standardize this feature. Every single EJB3 implementor will need an extra annotation to support that.
                And of course, delete-orphan was part of hibernate2!!
                Annotation makes no change in the way to use that feature compared to Hibernate core.