Entitymanager merge not updating record
Should Entity B be managed by two persistence contexts? Any other persistence provider returns a managed copy of Entity A in such situation, no matter if A is managed by another persistence context or detached. I am just pointing out that merge() is meant for merging DETACHED entity into a persistence context. ===================== How do you think Open JPA should behave in such situation?If we simply ignore managed entities in em.merge() than we must not insert the children into the database twice neither.Regarding 3.2.8: In this paragraph 'instance' clearly does not mean 'entity identity' but 'java identity' as indicated by the sentence: "The behavior is undefined if the same Java instance is made managed in more than one persistence context." In any case I'm not sure if 'fixing' this would help you, Arne. For all entities Y referenced by a relationship from X, if the relationship to Y has been annotated with the cascade element value cascade=PERSIST or cascade=ALL, the persist operation is applied to Y. In addition Entity B would be managed by Entity Manager X due to 3.2.4 Spec 3.24 ===================== The semantics of the flush operation, applied to an entity X are as follows: If X is a managed entity, it is synchronized to the database. Now Entity B would be managed by Entity Manager Y due to the cascading behavior you cited.================================= In the original test case, A (managed by em, not detached) is merged to em2 and there is no managed A in em2.
So even if you would get the entity merged into another EM in the 2nd request it would contain the old @Version and you will blow up at the end of the 2nd request with an Optimistic Lock Exception. There are now 2 ways to solve this issue in your application (even without changing Open JPA) 1.) Use a Servlet Filter to synchronize on your Session for those requests.
And hope that your @Transactional Interceptor can deal with it For JSF apps we might be able to register a hook for the redirect in the Faces Context. This might btw also work a bit different depending if you use synchronized or unsynchronized EM (see JTA-2.1 and Transaction()).
Summary: multi-page 'wizard' style pages without DTOs suck at this time.
In addition Entity B would be managed by Entity Manager X due to 3.2.4 Spec 3.24 ===================== The semantics of the flush operation, applied to an entity X are as follows: Btw. So you agree with me that it is a BUG that Open JPA tries to INSERT the entity and throws an exception due to the duplicate key? The behavior is undefined if the same Java instance is made managed in more than one persistence context.
Now Entity B would be managed by Entity Manager Y due to the cascading behavior you cited. And I come to the conclusion, that the only way to interpret it, is that managed entities of the SAME persistence context are meant. The spec also spelled out the following; ================================= 3.2.8 Managed Instances It is the responsibility of the application to insure that an instance is managed in only a single persistence context.