[Hibernate Envers] [Hibernate Version 5.4] EntityNotFound Exception when querying audits for a particular entity

There is an issue that I am facing where when I try to go to the audit page for an entity called DbConfig, the following error message is displayed on the screen called:

Case 1.
javax.persistence.EntityNotFoundException: Unable to find com.cloudera.cmf.model.DbRoleConfigGroup with id xxxx

I know there is fix available, [HHH-8051] - Hibernate JIRA to handle the exception gracefully but we can’t afford to upgrade Hibernate from 5.4 → 6.0 since it is a major upgrade and I think there would be breaking changes.

Case 2.
In the past, we also faced a similar issue when we visited the audit page for DbConfig but it was for a different entity (DbService):

javax.persistence.EntityNotFoundException: Unable to find com.cloudera.cmf.model.DbService with id xxx

But we resolved the above error by developing a workaround, basically a script that removes all the “bad revisions” from the database.

To understand what do I mean by “bad revisions”, I will first explain the associations.

DbService <---one-to-many---> DbRoleConfigGroup

DbService <--one-to-many--> DbConfig

DbRoleConfigGroup <--one-to-many--> DbConfig

For Case 2. We had the following hypothesis:
Envers assumes that revision IDs are strictly ascending over time. However for reasons we have not been able to pinpoint, Hibernate has generated some of the revision IDs out of order. This means that in some cases, a DbConfig change has a lower revision ID than the revision id of the creation of the DbService with which it is associated. It is this particular case that causes the exception.

Using this we developed a script that removes such bad revisions and as a result the error was resolved.

For Case 1. we also removed “bad revisions” where a DbConfig change has a lower revision ID than the revision id of the creation of the DbRoleConfigGroup with which it is associated.
We did this by developing a same workaround as the previous one but unfortunately the error still persists.

Please provide pointers on how to debug Case 1. We tried enabling logging on hibernate side as well but we don’t anything suspicious.

The stacktrace is:

javax.persistence.EntityNotFoundException: Unable to find com.cloudera.cmf.model.DbRoleConfigGroup with id xxxx
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$JpaEntityNotFoundDelegate.handleEntityNotFound(EntityManagerFactoryBuilderImpl.java:163)
    at org.hibernate.proxy.AbstractLazyInitializer.checkTargetState(AbstractLazyInitializer.java:286)
    at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:181)
    at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:310)
    at org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor.intercept(ByteBuddyInterceptor.java:45)
    at org.hibernate.proxy.ProxyConfiguration$InterceptorDispatcher.intercept(ProxyConfiguration.java:95)
    at com.mypackage.model.DbRoleConfigGroup$HibernateProxy$UwLMJc5j.hashCode(Unknown Source)
    at java.util.Arrays.hashCode(Arrays.java:4146)
    at com.google.common.base.Objects.hashCode(Objects.java:77)
    at com.mypackage.model.DbConfig.hashCode(DbConfig.java:473)
    at org.hibernate.envers.internal.tools.Triple.hashCode(Triple.java:63)
    at java.util.HashMap.hash(HashMap.java:339)
    at java.util.HashMap.put(HashMap.java:612)
    at org.hibernate.envers.internal.reader.FirstLevelCache.putOnEntityNameCache(FirstLevelCache.java:87)
    at org.hibernate.envers.internal.entities.EntityInstantiator.createInstanceFromVersionsEntity(EntityInstantiator.java:100)
    at org.hibernate.envers.query.internal.impl.RevisionsOfEntityQuery.getQueryResultRowValue(RevisionsOfEntityQuery.java:203)
    at org.hibernate.envers.query.internal.impl.RevisionsOfEntityQuery.getQueryResults(RevisionsOfEntityQuery.java:193)
    at org.hibernate.envers.query.internal.impl.RevisionsOfEntityQuery.list(RevisionsOfEntityQuery.java:136)
    at org.hibernate.envers.query.internal.impl.AbstractAuditQuery.getResultList(AbstractAuditQuery.java:112)

We also don’t know how to repro this by ourselves. We have the DB dump of our customer which we integrate to our product and visit the audit page DbConfig, only then we see the error.

Thanks in advance!

Your problem seems to be data related and very specific to your application. If you can’t reproduce this in isolation, there is nothing we can do to help you. The error is pretty clear, but what causes this situation is not comprehendible without debugging a reproducer step by step.

I was finally able to solve it.
Basically it was amalgamation of the following steps:

  1. Correcting the revision Ids of bad revisions for DbRoleConfigGroups entities that do exists.
  2. Deleting the revisions for DbRoleConfigGroups that don’t exists but still have audits present for them.