3 Replies Latest reply on Mar 28, 2009 6:07 AM by zhou jinliang

    Correctly delete a relational entity

    peter irmstadt Newbie

      Hi!


      I do have the following classes:


      @Entity
      @Table(name = "daDomain")
      @Name("domain")
      public class Domain implements Serializable
      {
      
           @Override
           public int hashCode() {
                ...
           }
      
           @Override
           public boolean equals(Object obj) { ... }
      
           
           private static final long serialVersionUID = 1L;
           private int id;
          private String name;
          private List<MailUser> mailuser;
          
          @DataModel
          @OneToMany(targetEntity = MailUser.class)
          @JoinColumn(name = "intDomainID", referencedColumnName = "intDomainID")
          public List<MailUser> getMailuser() {
                return mailuser;
           }
      
           public void setMailuser(List<MailUser> mailuser) {
                this.mailuser = mailuser;
           }
      
       
          @Id @GeneratedValue @Column(name="intDomainID")
          public int getId() {
              return id;
          }
      
          public void setId(int id) {
              this.id = id;
          }
      
          @Column(name="strDomain",unique=true)
          @Length(max=20,min=6)
          public String getName() {
              return name;
          }
      
          public void setName(String name) {
              this.name = name;
          }  
      }
      



      and


      @Entity
      @Table(name = "daMailUser")
      @Name("mailUser")
      @Scope(ScopeType.EVENT)
      public class MailUser implements Serializable
      {
           @Override
           public int hashCode() {..}
      
           @Override
           public boolean equals(Object obj) {..}
           private int id;
           private String username;
           private String password;
           private Domain domain;
           
           private static final long serialVersionUID = 1L;
           
           @Id @GeneratedValue
           @Column(name = "intMailUserID")
           public int getId() {
                return id;
           }
           public void setId(int id) {
                this.id = id;
           }
           @Column(name = "strUsername")
           public String getUsername() {
                return username;
           }
           public void setUsername(String username) {
                this.username = username;
           }
           @Column(name = "strPassword")
           public String getPassword() {
                return password;
           }
           public void setPassword(String password) {
                this.password = password;
           }
           
           @ManyToOne
           @JoinColumn (name = "intDomainID")
           public Domain getDomain() {
                return domain;
           }
           public void setDomain(Domain domain) {
                this.domain = domain;
           }
      }
      



      I do have a datatable filled with MailUsers. In an event scoped bean I defined a function


      public String delete(MailUser objMailuser)
           {
                objMailuser = em.merge(objMailuser);
                em.remove(objMailuser);
                em.flush();
           }



      while domain the current (injected) domain is.


      This procedure works sometimes, but most times (as I can see from hibernate log) the entity is deleted and then recreated (reinserted). Another issue is, that the datatable, that is bound to 'domain.mailUsers' isn't refresehd.


      Do you know what I can do to fix these two issues?


      Thank you in advance!