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

Hibernate JIRA | Adar Dembo | 4 years ago
  1. 0

    Use hibernate.envers.store_data_at_delete = true {code} class Parent { @Audited String name; Set<Child> children; } class Child { @Audited String name; @Audited Parent parent; public int hashCode() { return Objects.hashCode(name, parent); } } {code} Transaction 1: Creates a parent. Creates a child for it. You get one revision, one Parent_AUD (create), and one Child_AUD (create). Transaction 2: Deletes the parent. Deletes the child. In my case, this is via cascade. You get one revision, one Parent_AUD (delete), and one Child_AUD (delete). Transaction 3: Lookup using the following code: {code} AuditReaderFactory.get(em) .createQuery() .forRevisionsOfEntity(Child.class, true, true) .getResultList(); {code} This throws an exception: {noformat} javax.persistence.EntityNotFoundException: Unable to find com.cloudera.cmf.model.DbService 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 Parent_$$_javassist_2.hashCode(Parent_$$_javassist_2.java) at java.util.Arrays.hashCode(Arrays.java:3655) at com.google.common.base.Objects.hashCode(Objects.java:77) at Child.hashCode(Child.java:385) 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) ... {noformat} When store_data_at_delete is true, the second Child_AUD entry contains a link to Parent_AUD, instead of a NULL. The audit query tries to build Child entities out of the two Child_AUD entries but fails for the second one. Why does it fail? Because the SQL query it uses to fetch the correct Parent_AUD entry ignores entries with revision type DEL. Here's the corresponding comment from EntitiesAtRevisionQuery.list(): {code} /* * The query that we need to create: * SELECT new list(e) FROM versionsReferencedEntity e * WHERE * (all specified conditions, transformed, on the "e" entity) AND * (selecting e entities at revision :revision) * --> for DefaultAuditStrategy: * e.revision = (SELECT max(e2.revision) FROM versionsReferencedEntity e2 * WHERE e2.revision <= :revision AND e2.id = e.id) * * --> for ValidityAuditStrategy: * e.revision <= :revision and (e.endRevision > :revision or e.endRevision is null) * * AND * (only non-deleted entities) * e.revision_type != DEL */ {code} I think the right thing to do here is relax that last condition when we're fetching a relation for a DEL audit entry. Here's the corresponding forum post: https://community.jboss.org/message/800890

    Hibernate JIRA | 4 years ago | Adar Dembo
    javax.persistence.EntityNotFoundException: Unable to find com.cloudera.cmf.model.DbService with id 1
  2. 0

    Use hibernate.envers.store_data_at_delete = true {code} class Parent { @Audited String name; Set<Child> children; } class Child { @Audited String name; @Audited Parent parent; public int hashCode() { return Objects.hashCode(name, parent); } } {code} Transaction 1: Creates a parent. Creates a child for it. You get one revision, one Parent_AUD (create), and one Child_AUD (create). Transaction 2: Deletes the parent. Deletes the child. In my case, this is via cascade. You get one revision, one Parent_AUD (delete), and one Child_AUD (delete). Transaction 3: Lookup using the following code: {code} AuditReaderFactory.get(em) .createQuery() .forRevisionsOfEntity(Child.class, true, true) .getResultList(); {code} This throws an exception: {noformat} javax.persistence.EntityNotFoundException: Unable to find com.cloudera.cmf.model.DbService 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 Parent_$$_javassist_2.hashCode(Parent_$$_javassist_2.java) at java.util.Arrays.hashCode(Arrays.java:3655) at com.google.common.base.Objects.hashCode(Objects.java:77) at Child.hashCode(Child.java:385) 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) ... {noformat} When store_data_at_delete is true, the second Child_AUD entry contains a link to Parent_AUD, instead of a NULL. The audit query tries to build Child entities out of the two Child_AUD entries but fails for the second one. Why does it fail? Because the SQL query it uses to fetch the correct Parent_AUD entry ignores entries with revision type DEL. Here's the corresponding comment from EntitiesAtRevisionQuery.list(): {code} /* * The query that we need to create: * SELECT new list(e) FROM versionsReferencedEntity e * WHERE * (all specified conditions, transformed, on the "e" entity) AND * (selecting e entities at revision :revision) * --> for DefaultAuditStrategy: * e.revision = (SELECT max(e2.revision) FROM versionsReferencedEntity e2 * WHERE e2.revision <= :revision AND e2.id = e.id) * * --> for ValidityAuditStrategy: * e.revision <= :revision and (e.endRevision > :revision or e.endRevision is null) * * AND * (only non-deleted entities) * e.revision_type != DEL */ {code} I think the right thing to do here is relax that last condition when we're fetching a relation for a DEL audit entry. Here's the corresponding forum post: https://community.jboss.org/message/800890

    Hibernate JIRA | 4 years ago | Adar Dembo
    javax.persistence.EntityNotFoundException: Unable to find com.cloudera.cmf.model.DbService with id 1
  3. 0

    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: {code} 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 { ... } {code} Now suppose we issue three transactions: # Transaction 1 creates and persists a Foo and a Bar. # Transaction 2 deletes both the Foo and the Bar. # Transaction 3 queries for all revisions. The query will throw an exception like so: {noformat} 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) ... {noformat} It would be nice to fail gracefully in the face of non-existent entity targets, by nullifying the relation, perhaps.

    Hibernate JIRA | 4 years ago | Adar Dembo
    javax.persistence.EntityNotFoundException: Unable to find Bar with id 1
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

  5. 0

    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: {code} 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 { ... } {code} Now suppose we issue three transactions: # Transaction 1 creates and persists a Foo and a Bar. # Transaction 2 deletes both the Foo and the Bar. # Transaction 3 queries for all revisions. The query will throw an exception like so: {noformat} 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) ... {noformat} It would be nice to fail gracefully in the face of non-existent entity targets, by nullifying the relation, perhaps.

    Hibernate JIRA | 4 years ago | Adar Dembo
    javax.persistence.EntityNotFoundException: Unable to find Bar with id 1
  6. 0

    currently when querying audit data, if the related entity is not found, Envers will throw such exception: {code:title=Employee.java|borderStyle=solid} @Audited class Employee { private Integer id; private Company company; //many to one relation private String firstName; private String lastName; } @Audited class Company { private Integer id; private String name; } {code} 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(Ejb3Configuration.java:155) at org.hibernate.proxy.AbstractLazyInitializer.checkTargetState(AbstractLazyInitializer.java:235) at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:171) at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:259) at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:185) at test.Company.hashCode(Company.java: 234) at org.apache.commons.lang3.builder.HashCodeBuilder.append(HashCodeBuilder.java:850) at test.Employee.hashCode(Company.java:222) 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 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: https://community.jboss.org/message/801783

    Hibernate JIRA | 4 years ago | Jason Huang
    javax.persistence.EntityNotFoundException: Unable to find test.Company with id 10

    2 unregistered visitors
    Not finding the right solution?
    Take a tour to get the most out of Samebug.

    Tired of useless tips?

    Automated exception search integrated into your IDE

    Root Cause Analysis

    1. javax.persistence.EntityNotFoundException

      Unable to find com.cloudera.cmf.model.DbService with id 1

      at org.hibernate.ejb.Ejb3Configuration$Ejb3EntityNotFoundDelegate.handleEntityNotFound()
    2. Hibernate EJB
      Ejb3Configuration$Ejb3EntityNotFoundDelegate.handleEntityNotFound
      1. org.hibernate.ejb.Ejb3Configuration$Ejb3EntityNotFoundDelegate.handleEntityNotFound(Ejb3Configuration.java:155)
      1 frame
    3. Hibernate
      JavassistLazyInitializer.invoke
      1. org.hibernate.proxy.AbstractLazyInitializer.checkTargetState(AbstractLazyInitializer.java:171)
      2. org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:160)
      3. org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:195)
      4. org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:185)
      4 frames
    4. Unknown
      Parent_$$_javassist_2.hashCode
      1. Parent_$$_javassist_2.hashCode(Parent_$$_javassist_2.java)
      1 frame
    5. Java RT
      Arrays.hashCode
      1. java.util.Arrays.hashCode(Arrays.java:3655)
      1 frame
    6. Guava
      Objects.hashCode
      1. com.google.common.base.Objects.hashCode(Objects.java:77)
      1 frame
    7. Unknown
      Child.hashCode
      1. Child.hashCode(Child.java:385)
      1 frame
    8. A Hibernate O/RM Module
      Triple.hashCode
      1. org.hibernate.envers.tools.Triple.hashCode(Triple.java:74)
      1 frame
    9. Java RT
      HashMap.put
      1. java.util.HashMap.put(HashMap.java:372)
      1 frame
    10. A Hibernate O/RM Module
      AbstractAuditQuery.getResultList
      1. org.hibernate.envers.reader.FirstLevelCache.putOnEntityNameCache(FirstLevelCache.java:87)
      2. org.hibernate.envers.entities.EntityInstantiator.createInstanceFromVersionsEntity(EntityInstantiator.java:104)
      3. org.hibernate.envers.query.impl.RevisionsOfEntityQuery.list(RevisionsOfEntityQuery.java:134)
      4. org.hibernate.envers.query.impl.AbstractAuditQuery.getResultList(AbstractAuditQuery.java:105)
      4 frames