Issue Type: New Feature New Feature
Affects Versions: 4.1.4
Assignee: Unassigned
Components: envers
Created: 05/Mar/13 11:58 AM
Description:

If you use targetAuditMode=RelationTargetAuditMode.NOT_AUDITED on a relation and delete the (non-audited) target entity, queries for audits that contain links to the deleted target entity will throw an exception. Where it is thrown depends on how the source entity is implemented: if it implements hashCode() and includes the target entity in the hash, it'll be deep within the query itself.

Here's a concrete example:

class Foo {
  @Audited(withModifiedFlag=true)
  String interestingField;

  @Audited(targetAuditMode=RelationTargetAuditMode.NOT_AUDITED)
  Bar myBar;

  public boolean equals(Object obj) {
    // compares interestingField and myBar
  }

  public int hashCode() {
    // hashes interestingField and myBar
  }
}


class Bar {
  ...
}

Now suppose we issue three transactions:

  1. Transaction 1 creates and persists a Foo and a Bar.
  2. Transaction 2 deletes both the Foo and the Bar.
  3. Transaction 3 queries for all revisions.

The query will throw an exception like so:

javax.persistence.EntityNotFoundException: Unable to find Bar with id 1
          at org.hibernate.ejb.Ejb3Configuration$Ejb3EntityNotFoundDelegate.handleEntityNotFound(Ejb3Configuration.java:155)
          at org.hibernate.proxy.AbstractLazyInitializer.checkTargetState(AbstractLazyInitializer.java:171)
          at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:160)
          at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:195)
          at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:185)
          ...
          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)
        ...

It would be nice to fail gracefully in the face of non-existent entity targets, by nullifying the relation, perhaps.

Project: Hibernate ORM
Priority: Major Major
Reporter: Adar Dembo
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira