bug #6687

Updated by Andreas Kohlbecker about 3 years ago

The `eu.etaxonomy.vaadin.mvp.AbstractCdmEditorPresenter<DTO extends CdmBase, V extends ApplicationView<?>>` class uses short running sessions. Modified entities must thus be merged into the new Session before they can be saved.
In some cases however the merge fails with an IllegalStateException like the one that is shown below. The below exception was thrown after while merging a BotanicalName instance whereas the Person (id:10) is at the same time the *combinationAuthor* and the author of the associated *nomenclaturalReference* :

Caused by: java.lang.IllegalStateException: Multiple representations of the same entity [eu.etaxonomy.cdm.model.agent.Person#10] are being merged. Detached: [Cheng-Long Shaw]; Managed: [Cheng-Long Shaw]
at org.hibernate.event.internal.EntityCopyNotAllowedObserver.entityCopyDetected(
at org.hibernate.event.internal.MergeContext.put(
at org.hibernate.event.internal.DefaultMergeEventListener.entityIsDetached(
at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(
at org.hibernate.internal.SessionImpl.fireMerge(
at org.hibernate.internal.SessionImpl.merge(
at org.hibernate.engine.spi.CascadingActions$6.cascade(

This problem is a known issue of hibernate: [Hibernate ORMHHH-9106 - Multiple representations of the same entity cannot be merged using cascade=merge](

The **chosen solution** is to use the `ConversationHolder` to implement the session-per-conversation pattern in that way that a independent per view conversations exists for the whole lifetime of a eu.etaxonomy.vaadin.mvp.AbstractView. This covers read only view as well as popup editors implementing `eu.etaxonomy.vaadin.mvp.AbstractPopupView`. By this we completely avoid the merging problem since all instances are guaranteed to be loaded in the same session.



Since Hibernate 4.3.6 there is a hibernate property to work around this problem:

<prop key="hibernate.event.merge.entity_copy_observer">allow</prop>

When using `hibernate.event.merge.entity_copy_observer=allow` you must be aware of the risks and possible side effects, please refer to the patagraphs **RISKS OF MERGING ENTITY COPIES** and **RECOMMENDATIONS** in the above linked issue.

According to the recommendation given in the hibernate issue we should set `hibernate.event.merge.entity_copy_observer=log ` and add the following lines to the, in development environments (~/.cdmLibrary/

# Keep this on DEBUG for developing cdm-vaadin applications !!!
# logs merging of multiple representations of the same entity, please refer to
# for further details


Add picture from clipboard (Maximum size: 40 MB)