Working on a web application built on Spring 3.1.2.RELEASE, Hibernate Core 4.1.6.Final, Hibernate search 4.1.1.Final, Hibernate validator 4.3.0.Final, Aspectjrt 1.6.2.
Started getting this error on a ManyToMany relation after upgraded the Hibernate libraries to 4.1.6.Final from 3.6.3.Final. I came across some people suggesting to revert back to earlier versions of Hibernate. That solution doesn't work for me.
public class List.... { @ManyToMany(fetch = FetchType.EAGER) @JoinTable(name="LIST_ITEMS", joinColumns = @JoinColumn(name="LIST_ID"), inverseJoinColumns=@JoinColumn(name="ITEM_ID")) @Cascade({org.hibernate.annotations.CascadeType.ALL}) @Sort(type = SortType.NATURAL) @Cache(usage=CacheConcurrencyStrategy.READ_WRITE) @DisplayableCollection(displayName="Items") @IndexedEmbedded private Set<Item> listItems= new TreeSet<Item>(); }
public class Item.. { @ManyToMany(mappedBy = "listItems", fetch = FetchType.LAZY) @LazyField(useApplicationLazyLoad = false) private Set<List> lists= new HashSet<List>(); }
The error occurs when I try to add an Item that is already added to another List, to a New List and try to save the New List. List1:Item1 List2:Item1
Stacktrace:
org.springframework.dao.InvalidDataAccessApiUsageException: An entity copy was already assigned to a different entity.; nested exception is java.lang.IllegalStateException: An entity copy was already assigned to a different entity. at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:298) at org.springframework.orm.jpa.aspectj.JpaExceptionTranslatorAspect.ajc$afterThrowing$org_springframework_orm_jpa_aspectj_JpaExceptionTranslatorAspect$1$18a1ac9(JpaExceptionTranslatorAspect.aj:15) at xx.xx.xx.dao.jpa.JPA.merge(GenericJpaDao.java:105) at xx.xx.xx.model.AbsPerEnt.merge_aroundBody38(AbsPerEnt.java:311) at xx.xx.xx.model.AbsPerEnt$AjcClosure39.run(AbsPerEnt.java:1) at org.aspectj.runtime.reflect.JoinPointImpl.proceed(JoinPointImpl.java:101) at xx.xx.xx.aspect.ApplicationContextInjectorAspect.callInstanceCrudMethod(ApplicationContextInjectorAspect.java:51) at xx.xx.xx.model.AbsPerEnt.merge(AbsPerEnt.java:31) at xx.xx.xx.model.AbsPerEnt.merge_aroundBody6(AbsPerEnt.java:14) at xx.xx.xx.model.AbsPerEnt$AjcClosure7.run(AbsPerEnt.java:1) at org.aspectj.runtime.reflect.JoinPointImpl.proceed(JoinPointImpl.java:101) at xx.xx.xx.aspect.ApplicationContextInjectorAspect.callInstanceCrudMethod(ApplicationContextInjectorAspect.java:51) at xx.xx.xx.model.AbsPerEnt.merge(AbsPerEnt.java:14) at xx.xx.xx.model.AbsPerEnt.persistOrUpdate(AbsPerEnt.java:19) at xx.xx.xx.web.AbsBackBean.save(AbsBackBean.java:81) at xx.xx.xx.web.AbsBackBean.saveObject(AbsBackBean.java:77) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.jboss.seam.util.Reflections.invoke(Reflections.java:22) at org.jboss.seam.intercept.RootInvocationContext.proceed(RootInvocationContext.java:32) at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:56) at org.jboss.seam.transaction.RollbackInterceptor.aroundInvoke(RollbackInterceptor.java:28) at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68) at org.jboss.seam.core.BijectionInterceptor.aroundInvoke(BijectionInterceptor.java:77) at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68) at org.jboss.seam.core.MethodContextInterceptor.aroundInvoke(MethodContextInterceptor.java:44) at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68) at org.jboss.seam.core.SynchronizationInterceptor.aroundInvoke(SynchronizationInterceptor.java:32) at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
Caused by: java.lang.IllegalStateException: An entity copy was already assigned to a different entity. at org.hibernate.event.internal.EventCache.put(EventCache.java:184) at org.hibernate.event.internal.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:285) at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:151) at org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:914) at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:896) at org.hibernate.engine.spi.CascadingAction$6.cascade(CascadingAction.java:288) at org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:380) at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:323) at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:208) at org.hibernate.engine.internal.Cascade.cascadeCollectionElements(Cascade.java:409) at org.hibernate.engine.internal.Cascade.cascadeCollection(Cascade.java:350) at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:326) at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:208) at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:165) at org.hibernate.event.internal.DefaultMergeEventListener.cascadeOnMerge(DefaultMergeEventListener.java:439) at org.hibernate.event.internal.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:308) at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:151) at org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:914) at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:896) at org.hibernate.engine.spi.CascadingAction$6.cascade(CascadingAction.java:288) at org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:380) at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:323) at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:208) at org.hibernate.engine.internal.Cascade.cascadeCollectionElements(Cascade.java:409) at org.hibernate.engine.internal.Cascade.cascadeCollection(Cascade.java:350) at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:326) at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:208) at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:165) at org.hibernate.event.internal.DefaultMergeEventListener.cascadeOnMerge(DefaultMergeEventListener.java:439) at org.hibernate.event.internal.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:308) at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:151) at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:76) at org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:904) at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:888) at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:892)
|