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} 2016-08-23 06:40:39,114 ERROR [DatabaseImportWorker 5] [ORMPersister] Unexpected PersistenceLayerException, transaction rollback will be performed javax.persistence.EntityNotFoundException: Unable to find com.heiler.ppm.article.db.model.ArticleAttributeStructureGroupAttributeMap 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. |
|