javax.persistence.EntityNotFoundException: Unable to find com.xyz.entity.Child with id 311218356

Hibernate JIRA | Markus | 3 months ago
  1. 0

    I have a child entity that has two parent entities (the two parents itself have another common parent, but for simplicity I focus only on the three entities): Parent: {code} @OneToMany( mappedBy = "Parent", cascade = CascadeType.ALL, fetch = FetchType.LAZY ) @Fetch( FetchMode.SUBSELECT ) private Set< Child > children = new HashSet< Child >(); {code} OtherParent: {code} @OneToMany( mappedBy = "OtherParent", cascade = CascadeType.ALL, fetch = FetchType.LAZY ) @Fetch( FetchMode.SUBSELECT ) private Set< Child > children = new HashSet< Child >(); {code} Child: {code} @ManyToOne( fetch = FetchType.LAZY, optional = false ) @JoinColumn( name = "`ParentID`", nullable = false ) private Parent parent = null; @ManyToOne( fetch = FetchType.LAZY, optional = false ) @JoinColumn( name = "`OtherParentID`", nullable = false ) private OtherParent otherParent = null; {code} I use EntityManager.merge() to apply my changes: - if all objects are new, everything is fine - if OtherParent exists and Parent and Child are new, everything is fine - if OtherParent and Parent exist and Child is new, everything is fine However if Parent already exists and OtherParent and Child are new, I get an EntityNotFoundException because directly after cascadeOnMerge() Parent tries to load Child (which INSERT has not been executed yet) in copyValues(): {code} 04:32:14,798 TRACE [DefaultInitializeCollectionEventListener] Collection initialized 04:32:14,799 TRACE [CascadingAction] Cascading to merge: com.xyz.entity.Parent 04:32:14,799 TRACE [AbstractSaveEventListener] Persistent instance of: com.xyz.entity.Parent 04:32:14,799 TRACE [DefaultMergeEventListener] Ignoring persistent instance 04:32:14,799 TRACE [Cascade] Processing cascade ACTION_MERGE for: com.xyz.entity.Parent 04:32:14,799 TRACE [Cascade] Cascade ACTION_MERGE for collection: com.xyz.entity.Parent.children 04:32:14,799 TRACE [CascadingAction] Cascading to merge: com.xyz.entity.Child 04:32:14,799 TRACE [AbstractSaveEventListener] Transient instance of: com.xyz.entity.Child 04:32:14,799 TRACE [DefaultMergeEventListener] Merging transient instance 04:32:14,799 DEBUG [AbstractSaveEventListener] Generated identifier: 312220248, using strategy: HibernateSequenceGenerator 04:32:14,799 TRACE [AbstractSaveEventListener] Saving [com.xyz.entity.Child#312220248] 04:32:14,800 TRACE [ActionQueue] Adding an EntityInsertAction for [com.xyz.entity.Child] object 04:32:14,800 TRACE [ActionQueue] Adding insert with non-nullable, transient entities; insert=[EntityInsertAction[com.xyz.entity.Child#312220248]], dependencies=[NonNullableTransientDependencies[transientEntityName=com.xyz.entity.OtherParent requiredBy=[otherParent]]] 04:32:14,800 TRACE [UnresolvedEntityInsertActions] Adding insert with non-nullable, transient entities; insert=[EntityInsertAction[com.xyz.entity.Child#312220248]], dependencies=[NonNullableTransientDependencies[transientEntityName=com.xyz.entity.OtherParent requiredBy=[otherParent]]] 04:32:14,800 TRACE [Cascade] Done cascade ACTION_MERGE for collection: com.xyz.entity.Parent.children 04:32:14,800 TRACE [Cascade] Done processing cascade ACTION_MERGE for: com.xyz.entity.Parent 04:32:14,800 TRACE [DefaultLoadEventListener] Loading entity: [com.xyz.entity.Child#312220248] 04:32:14,800 TRACE [DefaultLoadEventListener] Attempting to resolve: [com.xyz.entity.Child#312220248] 04:32:14,800 TRACE [DefaultLoadEventListener] Object not resolved in any cache: [com.xyz.entity.Child#312220248] 04:32:14,800 TRACE [AbstractEntityPersister] Fetching entity: [com.xyz.entity.Child#312220248] 04:32:14,800 DEBUG [Loader] Static select for entity com.xyz.entity.Child [NONE:-1]: select ....... 04:32:14,800 DEBUG [Loader] Loading entity: [com.xyz.entity.Child#312220248] 04:32:14,801 TRACE [Loader] Done processing result set (0 rows) 04:32:14,801 TRACE [Loader] Total objects hydrated: 0 04:32:14,801 TRACE [StatefulPersistenceContext] Initializing non-lazy collections 04:32:14,801 DEBUG [Loader] Done entity load {code} Stack trace: {code} javax.persistence.EntityNotFoundException: Unable to find com.xyz.entity.Child with id 311218356 at org.hibernate.ejb.Ejb3Configuration$Ejb3EntityNotFoundDelegate.handleEntityNotFound(Ejb3Configuration.java:158) at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:214) at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:264) at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:152) at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1053) at org.hibernate.internal.SessionImpl.internalLoad(SessionImpl.java:980) at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:714) at org.hibernate.type.EntityType.resolve(EntityType.java:502) at org.hibernate.type.EntityType.replace(EntityType.java:366) at org.hibernate.type.CollectionType.replaceElements(CollectionType.java:517) at org.hibernate.type.CollectionType.replace(CollectionType.java:667) at org.hibernate.type.TypeHelper.replace(TypeHelper.java:177) at org.hibernate.event.internal.DefaultMergeEventListener.copyValues(DefaultMergeEventListener.java:401) at org.hibernate.event.internal.DefaultMergeEventListener.entityIsPersistent(DefaultMergeEventListener.java:213) at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:186) at org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:843) at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:825) at org.hibernate.engine.spi.CascadingAction$6.cascade(CascadingAction.java:288) at org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:396) at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:339) at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:207) at org.hibernate.engine.internal.Cascade.cascadeCollectionElements(Cascade.java:426) at org.hibernate.engine.internal.Cascade.cascadeCollection(Cascade.java:366) at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:342) at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:207) at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:164) at org.hibernate.event.internal.DefaultMergeEventListener.cascadeOnMerge(DefaultMergeEventListener.java:468) at org.hibernate.event.internal.DefaultMergeEventListener.entityIsPersistent(DefaultMergeEventListener.java:212) at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:186) at org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:843) at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:825) at org.hibernate.engine.spi.CascadingAction$6.cascade(CascadingAction.java:288) at org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:396) at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:339) at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:207) at org.hibernate.engine.internal.Cascade.cascadeCollectionElements(Cascade.java:426) at org.hibernate.engine.internal.Cascade.cascadeCollection(Cascade.java:366) at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:342) at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:207) at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:164) at org.hibernate.event.internal.DefaultMergeEventListener.cascadeOnMerge(DefaultMergeEventListener.java:468) at org.hibernate.event.internal.DefaultMergeEventListener.entityIsPersistent(DefaultMergeEventListener.java:212) at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:186) at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:86) at org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:833) at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:817) at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:821) at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:889) {code} Please advise how to further troubleshoot this issue, thank you.

    Hibernate JIRA | 3 months ago | Markus
    javax.persistence.EntityNotFoundException: Unable to find com.xyz.entity.Child with id 311218356
  2. 0

    Hibernate 4.2 OneToMany EntityNotFoundException

    Stack Overflow | 3 years ago | wsams
    org.springframework.orm.jpa.JpaObjectRetrievalFailureException: Unable to find com.example.metadata.xkbml.entity.Keyword with id hello; nested exception is javax.persistence.EntityNotFoundException: Unable to find com.example.metadata.xkbml.entity.Keyword with id hello
  3. 0

    Spring cannot find entity when running threads in unit test

    Stack Overflow | 2 years ago | Thomas Uhrig
    javax.persistence.EntityNotFoundException: Unable to find BankConnection with id 1003
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

  5. 0

    Spring Data JPA JpaObjectRetrievalFailureException occurring after deleting ManyToOne record

    Stack Overflow | 4 years ago | ssn771
    javax.persistence.EntityNotFoundException: Unable to find com.test.dailyemail.repository.NewsLink with id 8
  6. 0

    javax.persistence.EntityNotFoundException: Unable to find org.broadleafcommerce.profile.core.domain.CustomerImpl with id 253104

    Stack Overflow | 3 years ago | Lucky
    javax.persistence.EntityNotFoundException: Unable to find org.broadleafcommerce.profile.core.domain.CustomerImpl with id 253104 STACKTRACE: Posting only important lines of stacktrace for readability: [artifact:mvn] org.broadleafcommerce.core.payment.service.exception.PaymentException: CompositePaymentServiceImpl - Unable to execute payment for order -- id: null

    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.xyz.entity.Child with id 311218356

      at org.hibernate.ejb.Ejb3Configuration$Ejb3EntityNotFoundDelegate.handleEntityNotFound()
    2. Hibernate EJB
      Ejb3Configuration$Ejb3EntityNotFoundDelegate.handleEntityNotFound
      1. org.hibernate.ejb.Ejb3Configuration$Ejb3EntityNotFoundDelegate.handleEntityNotFound(Ejb3Configuration.java:158)
      1 frame
    3. Hibernate
      SessionImpl.merge
      1. org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:214)
      2. org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:264)
      3. org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:152)
      4. org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1053)
      5. org.hibernate.internal.SessionImpl.internalLoad(SessionImpl.java:980)
      6. org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:714)
      7. org.hibernate.type.EntityType.resolve(EntityType.java:502)
      8. org.hibernate.type.EntityType.replace(EntityType.java:366)
      9. org.hibernate.type.CollectionType.replaceElements(CollectionType.java:517)
      10. org.hibernate.type.CollectionType.replace(CollectionType.java:667)
      11. org.hibernate.type.TypeHelper.replace(TypeHelper.java:177)
      12. org.hibernate.event.internal.DefaultMergeEventListener.copyValues(DefaultMergeEventListener.java:401)
      13. org.hibernate.event.internal.DefaultMergeEventListener.entityIsPersistent(DefaultMergeEventListener.java:213)
      14. org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:186)
      15. org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:843)
      16. org.hibernate.internal.SessionImpl.merge(SessionImpl.java:825)
      17. org.hibernate.engine.spi.CascadingAction$6.cascade(CascadingAction.java:288)
      18. org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:396)
      19. org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:339)
      20. org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:207)
      21. org.hibernate.engine.internal.Cascade.cascadeCollectionElements(Cascade.java:426)
      22. org.hibernate.engine.internal.Cascade.cascadeCollection(Cascade.java:366)
      23. org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:342)
      24. org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:207)
      25. org.hibernate.engine.internal.Cascade.cascade(Cascade.java:164)
      26. org.hibernate.event.internal.DefaultMergeEventListener.cascadeOnMerge(DefaultMergeEventListener.java:468)
      27. org.hibernate.event.internal.DefaultMergeEventListener.entityIsPersistent(DefaultMergeEventListener.java:212)
      28. org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:186)
      29. org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:843)
      30. org.hibernate.internal.SessionImpl.merge(SessionImpl.java:825)
      31. org.hibernate.engine.spi.CascadingAction$6.cascade(CascadingAction.java:288)
      32. org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:396)
      33. org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:339)
      34. org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:207)
      35. org.hibernate.engine.internal.Cascade.cascadeCollectionElements(Cascade.java:426)
      36. org.hibernate.engine.internal.Cascade.cascadeCollection(Cascade.java:366)
      37. org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:342)
      38. org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:207)
      39. org.hibernate.engine.internal.Cascade.cascade(Cascade.java:164)
      40. org.hibernate.event.internal.DefaultMergeEventListener.cascadeOnMerge(DefaultMergeEventListener.java:468)
      41. org.hibernate.event.internal.DefaultMergeEventListener.entityIsPersistent(DefaultMergeEventListener.java:212)
      42. org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:186)
      43. org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:86)
      44. org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:833)
      45. org.hibernate.internal.SessionImpl.merge(SessionImpl.java:817)
      46. org.hibernate.internal.SessionImpl.merge(SessionImpl.java:821)
      46 frames
    4. Hibernate EJB
      AbstractEntityManagerImpl.merge
      1. org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:889)
      1 frame