Type: New Feature
Resolution: Out of Date
Affects versions: 4.2.0.Final
Fix versions: None
Environment:Hibernate 4.2.0.Final, Oracle 11g or SQL Server 2008, Jboss 7.1.1
Last commented by a user?:true
currently when querying audit data, if the related entity is not found, Envers will throw such exception:
Unable to find test.Company with id 10: javax.persistence.EntityNotFoundException: Unable to find test.Company with id 10
at test.Company.hashCode(Company.java: 234)
It will be better to give an option to not throw exception in this case, because this can happen in real world. 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? Then it happens again. This is a real case in an existing project where 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.
See the thread in forum: