Per Spec: ==================== 18.104.22.168 Merging Detached Entity State The merge operation allows for the propagation of state from detached entities onto persistent entities managed by the entity manager. Since A is marked managed and not managed in em2, Open JPA creates a new managed instance in em2 and eventually "INSERT" to the database.The semantics of the merge operation applied to an entity X are as follows: ........ I am not saying this is the right or wrong behavior but this use case is undefined.
For an introductory article to Hibernate, visit our tutorial on Hibernate 4 with Spring. We should also understand the history of Hibernate development that led to some partly duplicated API methods.Hibernate was the most successful Java ORM implementation.No wonder that the specification for Java persistence API (JPA) was heavily influenced by the Hibernate API.The semantics of the merge operation applied to an entity X are as follows: ........ And I come to the conclusion, that the only way to interpret it, is that managed entities of the SAME persistence context are meant. However, the child relationship is still processed if proper cascade is specified.• If X is a managed entity, it is ignored by the merge operation, however, the merge operation is cascaded to entities referenced by relationships from X if these relationships have been annotated with the cascade element value cascade=MERGE or cascade=ALL annotation. 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 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.