We're updating the issue view to help you get more done. 

targetAuditMode=RelationTargetAuditMode.NOT_AUDITED should allow for missing entity targets

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:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 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:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 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.

Environment

None

Status

Assignee

Chris Cranford

Reporter

Adar Dembo

Fix versions

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

5.2.8
4.1.4

Priority

Major