Add the ability to handle related entity not found gracefully


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 org.hibernate.ejb.Ejb3Configuration$Ejb3EntityNotFoundDelegate.handleEntityNotFound(
at org.hibernate.proxy.AbstractLazyInitializer.checkTargetState(
at org.hibernate.proxy.AbstractLazyInitializer.initialize(
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(
at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(
at test.Company.hashCode( 234)
at org.apache.commons.lang3.builder.HashCodeBuilder.append(
at test.Employee.hashCode(
at java.util.HashMap.put(
at org.hibernate.envers.reader.FirstLevelCache.putOnEntityNameCache(
at org.hibernate.envers.entities.EntityInstantiator.createInstanceFromVersionsEntity(
at org.hibernate.envers.query.impl.RevisionsOfEntityQuery.list(
at org.hibernate.envers.query.impl.AbstractAuditQuery.getResultList(

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:


Hibernate 4.2.0.Final, Oracle 11g or SQL Server 2008, Jboss 7.1.1


Chris Cranford


Jason Huang

Fix versions




Suitable for new contributors


Requires Release Note


Pull Request





Affects versions