1 Reply Latest reply on Nov 28, 2012 8:39 AM by Adam Warski

    How to audit an auto relationship?

    Marco Rodrigues Newbie

      Hi everyone,

       

      I'm new in town and Envers too

       

      I have had some problems with auto relationship. I will try to explain below, so be patiente, my English is not so good:

       

      Using JBoss 6.1AS with hibernate 3.6.6, Envers 3.6.10.Final and Oracle 11g

       

       

      @Entity
      @Audited
      @Table(name = "FUNCAO")
      @SequenceGenerator(name = "SEQ", sequenceName = "SQ_ID_FUNCAO")
      @AttributeOverrides({           @AttributeOverride(name = "id", column = @Column(name = "ID_FUNCAO")), 
                                                                  @AttributeOverride(name = "descricao", column = @Column(name = "CO_FUNCAO")) })
      @Access(AccessType.FIELD)
      public class Funcao extends BeanDadoAuxiliar implements Serializable, Comparable<Funcao>{
      
      
                private static final long serialVersionUID = 6679561813614233454L;
      
      
                @ManyToOne(fetch = FetchType.LAZY)
                @JoinColumn(name="ID_TIPO_FUNCAO")
                private TipoFuncao tipoFuncao;
      
                  @ManyToOne(fetch = FetchType.LAZY)
                @JoinColumn(name="ID_FUNCAO_CORRELATA")
                private Funcao funcaoCorrelata;
      ....
      

       

      My problem is when I change property "funcaoCorrelata" and save pojo, envers only generate an audit to "root" class "Funcao".

      I have reading posts here and on stackoverflow and conclued that envers only save when occurs changes. Ok, it's space saving feature, but when I retrieve history from audit, i had a exception

       

       

      Caused by: javax.persistence.EntityNotFoundException: Unable to find (package excluded)...Funcao with id 5
      

       

      So, a pojo with id 5 is in normal table, but not in audited one, because envers do not save it. How can I solve this? Is there a chance to "join" both records in results? Here is my query code:

       

       

      AuditReader reader = AuditReaderFactory.get(super.getEntityManager());
      AuditQuery q = reader.createQuery().forRevisionsOfEntity(Funcao.class, false, true).addOrder(AuditEntity.revisionProperty("dataRevisao").desc());
      return q.getResultList();
      

       

      Regards,

       

      Marco

       

      Message was edited by: Marco Rodrigues My database is already loaded with a dump of older system! This colud lead to strange behavior that I got. With a clean DB, all inserts will be made by application and audited, so I think it will work. I will populate my adudited tables with values loaded form other system and report the results.