1 Reply Latest reply on Dec 20, 2008 12:35 PM by adamw

    No relationship revision information stored when getter for

    dunks80

      It seems that if a relationship's getter is annotated with @Audit no revision information is stored. See modified MixedAccessType testcase

      /*
       * Hibernate, Relational Persistence for Idiomatic Java
       *
       * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
       * indicated by the @author tags or express copyright attribution
       * statements applied by the authors. All third-party contributions are
       * distributed under license by Red Hat Middleware LLC.
       *
       * This copyrighted material is made available to anyone wishing to use, modify,
       * copy, or redistribute it subject to the terms and conditions of the GNU
       * Lesser General Public License, as published by the Free Software Foundation.
       *
       * This program is distributed in the hope that it will be useful,
       * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
       * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
       * for more details.
       *
       * You should have received a copy of the GNU Lesser General Public License
       * along with this distribution; if not, write to:
       * Free Software Foundation, Inc.
       * 51 Franklin Street, Fifth Floor
       * Boston, MA 02110-1301 USA
       */
      package org.hibernate.envers.test.integration.accesstype;
      
      import javax.persistence.CascadeType;
      import javax.persistence.Entity;
      import javax.persistence.FetchType;
      import javax.persistence.GeneratedValue;
      import javax.persistence.Id;
      import javax.persistence.JoinColumn;
      import javax.persistence.ManyToOne;
      import javax.persistence.Transient;
      
      import org.hibernate.envers.Audited;
      import org.hibernate.annotations.AccessType;
      
      /**
       * @author Adam Warski (adam at warski dot org)
       */
      @Entity
      
      public class MixedAccessTypeEntity {
       @Id
       @GeneratedValue
       private Integer id;
      
       @AccessType("property")
       private String data;
      
       @Transient
       private boolean dataSet;
      
       @Audited
       private String foo;
      
       @ManyToOne(fetch = FetchType.LAZY,cascade=CascadeType.ALL)
       @JoinColumn(name = "bar_id")
       private MixedAccessTypeEntity bar;
      
       public MixedAccessTypeEntity() {
       }
      
       public MixedAccessTypeEntity(String data,String foo) {
       this.data = data;
       this.foo=foo;
       }
      
       public MixedAccessTypeEntity(Integer id, String data,String foo) {
       this.id = id;
       this.data = data;
       this.foo=foo;
       }
      
       public Integer getId() {
       throw new RuntimeException();
       }
      
       public void setId(Integer id) {
       throw new RuntimeException();
       }
      
       // TODO: this should be on the property. But how to discover in AnnotationsMetadataReader that the
       // we should read annotations from fields, even though the access type is "property"?
       @Audited
       public String getData() {
       return data;
       }
      
       public void setData(String data) {
       this.data = data;
       dataSet = true;
       }
      
       public boolean isDataSet() {
       return dataSet;
       }
      
       public Integer readId() {
       return id;
       }
      
       public void writeData(String data) {
       this.data = data;
       }
      
       public boolean equals(Object o) {
       if (this == o) return true;
       if (!(o instanceof MixedAccessTypeEntity)) return false;
      
       MixedAccessTypeEntity that = (MixedAccessTypeEntity) o;
      
       if (data != null ? !data.equals(that.data) : that.data != null) return false;
       if (id != null ? !id.equals(that.id) : that.id != null) return false;
      
       return true;
       }
      
       public int hashCode() {
       int result;
       result = (id != null ? id.hashCode() : 0);
       result = 31 * result + (data != null ? data.hashCode() : 0);
       return result;
       }
      
       public String getFoo()
       {
       return foo;
       }
      
       public void setFoo(String foo)
       {
       this.foo = foo;
       }
      
       @Audited
       public MixedAccessTypeEntity getBar()
       {
       return bar;
       }
      
       public void setBar(MixedAccessTypeEntity bar)
       {
       this.bar = bar;
       }
      }
      


      /*
       * Hibernate, Relational Persistence for Idiomatic Java
       *
       * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
       * indicated by the @author tags or express copyright attribution
       * statements applied by the authors. All third-party contributions are
       * distributed under license by Red Hat Middleware LLC.
       *
       * This copyrighted material is made available to anyone wishing to use, modify,
       * copy, or redistribute it subject to the terms and conditions of the GNU
       * Lesser General Public License, as published by the Free Software Foundation.
       *
       * This program is distributed in the hope that it will be useful,
       * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
       * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
       * for more details.
       *
       * You should have received a copy of the GNU Lesser General Public License
       * along with this distribution; if not, write to:
       * Free Software Foundation, Inc.
       * 51 Franklin Street, Fifth Floor
       * Boston, MA 02110-1301 USA
       */
      package org.hibernate.envers.test.integration.accesstype;
      
      import java.util.Arrays;
      import javax.persistence.EntityManager;
      
      import org.hibernate.envers.test.AbstractEntityTest;
      import org.testng.annotations.BeforeClass;
      import org.testng.annotations.Test;
      
      import org.hibernate.ejb.Ejb3Configuration;
      
      /**
       * @author Adam Warski (adam at warski dot org)
       */
      public class MixedAccessType extends AbstractEntityTest {
       private Integer id1;
       private Integer barId;
       public void configure(Ejb3Configuration cfg) {
       cfg.addAnnotatedClass(MixedAccessTypeEntity.class);
       }
      
       @BeforeClass(dependsOnMethods = "init")
       public void initData() {
       EntityManager em = getEntityManager();
       em.getTransaction().begin();
       MixedAccessTypeEntity mate = new MixedAccessTypeEntity("data","foo");
       MixedAccessTypeEntity bar = new MixedAccessTypeEntity("bar","bar");
       mate.setBar(bar);
       em.persist(mate);
      
       id1 = mate.readId();
       barId=bar.readId();
       em.getTransaction().commit();
      
       em.getTransaction().begin();
       mate = em.find(MixedAccessTypeEntity.class, id1);
       mate.getBar().setFoo("bar2");
       mate.writeData("data2");
       mate.setFoo("bar");
       em.getTransaction().commit();
       }
      
       @Test
       public void testRevisionsCounts() {
       assert Arrays.asList(1, 2).equals(getAuditReader().getRevisions(MixedAccessTypeEntity.class, id1));
       assert Arrays.asList(1, 2).equals(getAuditReader().getRevisions(MixedAccessTypeEntity.class, barId));
       }
      
      
       @Test
       public void testHistoryOfId1() {
       MixedAccessTypeEntity ver1 = new MixedAccessTypeEntity(id1, "data","foo");
       MixedAccessTypeEntity ver2 = new MixedAccessTypeEntity(id1, "data2","foo");
      
       MixedAccessTypeEntity barRev1 = getAuditReader().find(MixedAccessTypeEntity.class, barId, 1);
       MixedAccessTypeEntity barRev2 = getAuditReader().find(MixedAccessTypeEntity.class, barId, 2);
       assert barRev1.getFoo().equals("bar");
       assert barRev2.getFoo().equals("bar2");
      
       MixedAccessTypeEntity rev1 = getAuditReader().find(MixedAccessTypeEntity.class, id1, 1);
       MixedAccessTypeEntity rev2 = getAuditReader().find(MixedAccessTypeEntity.class, id1, 2);
       //the relationship ends up being null
       assert rev1.getBar()!=null;
       assert rev1.getBar().equals(barRev1);
       assert rev1.getFoo().equals("foo");
       assert rev2.getFoo().equals("bar");
       assert rev1.isDataSet();
       assert rev2.isDataSet();
      
       assert rev1.equals(ver1);
       assert rev2.equals(ver2);
       }
      }
      


      Is this a bug or expected behavior?

        • 1. Re: No relationship revision information stored when getter
          adamw

          Hello,

          but you don't have:

          @AccessType("property")
          

          on the relation declaration:
          @ManyToOne(fetch =FetchType.LAZY,cascade=CascadeType.ALL)
          @JoinColumn(name = "bar_id")
          private MixedAccessTypeEntity bar;
          


          so the access mode for that field is "field", and no @Audited annotation is found there.

          --
          Adam