2 Replies Latest reply on May 23, 2013 11:33 AM by Adam Warski

    Auditing collection of components

    Julia . Newbie

      Hi,

       

      I'm trying to audit an entity which contains a collection of components. I know that auditing @ElementCollection wasn't supported in previous version of hibernate (https://hibernate.atlassian.net/browse/HHH-6613), therefore I'm using version 4.2.0.Final.

       

      My classes are:

       

       

      @Entity

      @Audited

      @Table(name = "TEST_OWNER")

      public class Owner {

                private Integer id;

                private String name;

                private Set<Owned> ownedSet;

       

                @Id

                @GeneratedValue(strategy=GenerationType.AUTO)

                @Column(name = "OWNER_ID")

                public Integer getId() {

                          return id;

                }

                public void setId(Integer id) {

                          this.id = id;

                }

       

                @Column(name = "OWNER_NAME")

                public String getName() {

                          return name;

                }

       

                public void setName(String name) {

                          this.name = name;

                }

       

                @ElementCollection

                @CollectionTable(name = "TEST_OWNER_PARAMS", joinColumns = @JoinColumn(name = "OWNER_ID"))

                public Set<Owned> getOwnedSet() {

                          return ownedSet;

                }

                public void setOwnedSet(Set<Owned> ownedSet) {

                          this.ownedSet = ownedSet;

                }

       

      }

       

       

       

      @Embeddable

      public class Owned {

                private String param;

                private String val;

       

                public Owned() {

                }

       

                public Owned(String param, String val) {

                          super();

                          this.param = param;

                          this.val = val;

                }

       

       

                @Column(name = "PAR")

                public String getParam() {

                          return param;

                }

                public void setParam(String param) {

                          this.param = param;

                }

       

                @Column(name = "VAL")

                public String getVal() {

                          return val;

                }

                public void setVal(String val) {

                          this.val = val;

                }

       

                @Override

                public int hashCode() {

                          final int prime = 31;

                          int result = 1;

                          result = prime * result + ((param == null) ? 0 : param.hashCode());

                          result = prime * result + ((val == null) ? 0 : val.hashCode());

                          return result;

                }

       

                @Override

                public boolean equals(Object obj) {

                          if (this == obj)

                                    return true;

                          if (obj == null)

                                    return false;

                          if (!(obj instanceof Owned))

                                    return false;

                          Owned other = (Owned) obj;

                          if (param == null) {

                                    if (other.param != null)

                                              return false;

                          }

                          else if (!param.equals(other.param))

                                    return false;

                          if (val == null) {

                                    if (other.val != null)

                                              return false;

                          }

                          else if (!val.equals(other.val))

                                    return false;

                          return true;

                }

      }

       

       

       

       

      Primary key for TEST_OWNER_PARAMS_AUD table is (OWNER_ID, PAR, VAL, REV, REVTYPE).

       

      Save/update/merge succeeds when I perform it on an Owner entity with "ownedSet" which contains a single element. However, save/update/merge fails when "ownedSet" contains more than one element (in case of merge - when more than one element in "ownedSet" was modified). The error I'm getting is:

       

      org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [TEST_OWNER_PARAMS_AUD#{REV=DefaultRevisionEntity(id = 2277, revisionDate = May 7, 2013 2:08:27 PM), Owner_id=2276, REVTYPE=ADD}]

       

      Why does it happen? Am I missing something?

       

       

      Thanks,

      Julia

        • 1. Re: Auditing collection of components
          Julia . Newbie

          I'm sorry, my message messed up, here are my classes again:

           

           

          @Entity
          @Audited
          @Table(name = "TEST_OWNER")
          public class Owner {
                    private Integer id;
                    private String name;
                    private Set<Owned> ownedSet;
            
                    @Id
                    @GeneratedValue(strategy=GenerationType.AUTO)
                    @Column(name = "OWNER_ID")
                    public Integer getId() {
                              return id;
                    }
                    public void setId(Integer id) {
                              this.id = id;
                    }
            
                    @Column(name = "OWNER_NAME")
                    public String getName() {
                              return name;
                    }
            
                    public void setName(String name) {
                              this.name = name;
                    }
            
                    @ElementCollection
                    @CollectionTable(name = "TEST_OWNER_PARAMS", joinColumns = @JoinColumn(name = "OWNER_ID"))
                    public Set<Owned> getOwnedSet() {
                              return ownedSet;
                    }
                    public void setOwnedSet(Set<Owned> ownedSet) {
                              this.ownedSet = ownedSet;
                    }
            
          }
          
          
          
          @Embeddable
          public class Owned {
                    private String param;
                    private String val;
            
                    public Owned() {
                    }
            
                    public Owned(String param, String val) {
                              super();
                              this.param = param;
                              this.val = val;
                    }
          
          
                    @Column(name = "PAR")
                    public String getParam() {
                              return param;
                    }
                    public void setParam(String param) {
                              this.param = param;
                    }
            
                    @Column(name = "VAL")
                    public String getVal() {
                              return val;
                    }
                    public void setVal(String val) {
                              this.val = val;
                    }
            
                    @Override
                    public int hashCode() {
                              final int prime = 31;
                              int result = 1;
                              result = prime * result + ((param == null) ? 0 : param.hashCode());
                              result = prime * result + ((val == null) ? 0 : val.hashCode());
                              return result;
                    }
            
                    @Override
                    public boolean equals(Object obj) {
                              if (this == obj)
                                        return true;
                              if (obj == null)
                                        return false;
                              if (!(obj instanceof Owned))
                                        return false;
                              Owned other = (Owned) obj;
                              if (param == null) {
                                        if (other.param != null)
                                                  return false;
                              }
                              else if (!param.equals(other.param))
                                        return false;
                              if (val == null) {
                                        if (other.val != null)
                                                  return false;
                              }
                              else if (!val.equals(other.val))
                                        return false;
                              return true;
                    }
          }
          
          
          
          • 2. Re: Auditing collection of components
            Adam Warski Master

            Hard to say, "should" work, but apperantely there's a bug somewhere.

             

            Please report in JIRA.

             

            Adam