3 Replies Latest reply on Mar 21, 2013 2:25 AM by Jason Huang

    Handle related entity not found gracefully

    Jason Huang Newbie

      Hi

       

      With Envers 4.1.6, currently when querying audit data, if the related entity is not found, Envers will throw such exception:

       

      @Audited
      class Employee {
        private Integer id;
        private Company company; //many to one relation
        private String firstName;
        private String lastName;
      }
      
      @Audited
      class Company {
        private Integer id;
        private String name;
      }
      

       

      Unable to find test.Company with id 10: javax.persistence.EntityNotFoundException: Unable to find test.Company with id 10
          at org.hibernate.ejb.Ejb3Configuration$Ejb3EntityNotFoundDelegate.handleEntityNotFound(Ejb3Configuration.java:155)
          at org.hibernate.proxy.AbstractLazyInitializer.checkTargetState(AbstractLazyInitializer.java:235)
          at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:171)
          at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:259)
          at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:185)
          at test.Company.hashCode(Company.java: 234)
          at org.apache.commons.lang3.builder.HashCodeBuilder.append(HashCodeBuilder.java:850)
          at test.Employee.hashCode(Company.java:222)
          at org.hibernate.envers.tools.Triple.hashCode(Triple.java:74)
          at java.util.HashMap.put(HashMap.java:372)
          at org.hibernate.envers.reader.FirstLevelCache.putOnEntityNameCache(FirstLevelCache.java:87)
          at org.hibernate.envers.entities.EntityInstantiator.createInstanceFromVersionsEntity(EntityInstantiator.java:104)
          at org.hibernate.envers.query.impl.RevisionsOfEntityQuery.list(RevisionsOfEntityQuery.java:134)
          at org.hibernate.envers.query.impl.AbstractAuditQuery.getResultList(AbstractAuditQuery.java:105)
      

       

      I think it's better to give an option to not throw exception in this case. In practise, this can happen, but is not someone's fault. For example, at first, only Employee is audited, and the company field in Employee is annotated as targetAuditMode=RelationTargetAuditMode.NOT_AUDITED. After the system has been running for several months, Company becomes audited as well. Then problem happens. When viewing Employee history, the above exception keeps happening, even if all current company entities have been put into audit table.

       

      The reason is when loading a revision of Employee and the related Company, Envers always tries to load Company with the max revision number equal or less than Employee's revision number. One possible workaround is to initialize Company audit table by populating all companies with revision number 0. However, what about if company has a relation with some other entities which has already been audited? I would be advised that I should audit all entities at the first beginning. Well, I'd love to do this in a brand new project, however, with old projects I can't do that because not all domain objects are JPA entities.

       

      Apart from this scenario, the problem can easily happen when implementing a purging feature. For example, a task runs weekly to purge history records older that 6 months. As the object graph can be complicated, it's hard to make sure there's no orphan relation in audit tables.

       

      In fact, the problem can be solved by handling relation not found gracefully. For instance, the strategy to look up relation could be:

      1. Look up related entity with the max revision number equal or less than the targeted entity's revision number.

      2. If not found, look up related entity with the min revision number larger than the targeted entity's revision number.

      3. If not found, look up related entity in "current" version.

      4. If not found, return null or empty entity with id field populated.

      This might break the concept of showing objects states in one specific moment. But I think it's important to make it work for these cases.

       

      Appreciate any idea.