bug #6687
closedMultiple representations of the same entity merge problem
100%
Description
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(EntityCopyNotAllowedObserver.java:37) at org.hibernate.event.internal.MergeContext.put(MergeContext.java:245) at org.hibernate.event.internal.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:304) at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:170) at org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:850) at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:832) at org.hibernate.engine.spi.CascadingActions$6.cascade(CascadingActions.java:260)
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.
OLD REJECTED SOLUTION DESCTIPTION BELOW
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 log4j.properties, in development environments (~/.cdmLibrary/log4j.properties)
# Keep this on DEBUG for developing cdm-vaadin applications !!! # # logs merging of multiple representations of the same entity, please refer to # https://dev.e-taxonomy.eu/redmine/issues/6687 # for further details log4j.logger.org.hibernate.event.internal.EntityCopyAllowedLoggedObserver=DEBUG
Related issues