0 Replies Latest reply on Jun 28, 2008 1:22 PM by Lukasz Marchewka

    remove entity which have an child

    Lukasz Marchewka Newbie

      Hej:) Sorry for my bad english:)


      A have entity:


      @Entity
      
      public class User implements Serializable{
      
      ....   //there are other field
      
             private DName name;
      
      ....
      
      
              @NotNull
      
              @ManyToOne
      
              @JoinColumn(name="name_id")
      
              public DName getName() {
      
                   return name;
      
              }
      
      
              public void setName(DName name) {
      
                   this.name = name;
      
              }
      
      ...
      
      }





      @Entity
      
      @Table(name="DICT_NAME")
      
      public class DName implements Serializable {
      
              private Integer id;
      
              private String value;
      
              @Id @GeneratedValue
      
              public Integer getId() {
      
                   return id;
      
              }
      
      
              public void setId(Integer id) {
      
                   this.id = id;
      
              }
      
              
      
              @Column(unique=true)
      
              @Length(max=20)
      
              @NotNull
      
              public String getValue() {
      
                   return value;
      
              }
      
      
              public void setValue(String value) {
      
                   this.value = value;
      
              }
      
      }



      So, a lot of users can have the same name.
      In SFSB, when I try remove user I must check that this name is use for other user or other entity, if no I want remove.


      I try did this for a lot of way.
      1) add cascade(ALL):


              @NotNull
      
              @Cascade(ALL)
      
              @ManyToOne
      
              @JoinColumn(name="name_id")
      
              public DName getName() {
      
                   return name;
      
              }


      but this work ony for OneToOne and OneToMany


      2) I try:


      em.remove(user);
      
      try {
      
         em.remove(name);
      
      } catch( //Exception if this name is use){}



      but exception is throw after end fuction, and i can't catch it.
      And this exception go to screen, and i want do some thing after that..


      3) Some solution is check if this name is use, but I use name in a lot of other entity, and this query will be wery looong...
      4) I try get manual transaction bud i don't know how...


      Mayby someone know solutnio:) Thanks, and sorry for my english:)