2010-11-30 210 views
1

這是我的問題描述。 「A」與另一個對象「C」具有OneToOne(cascadeType = cascadeType.All)關係。即使「B」具有OneToOne(cascadeType = cascadeType.All)與「C」的關係。我堅持對象「A」,我需要堅持對象「B」。我可以成功堅持對象「A」,但是當我堅持「 B」我得到的異常休眠問題(javax.persistence.PersistenceException:org.hibernate.PersistentObjectException)

javax.persistence.PersistenceException: org.hibernate.PersistentObjectException 

之所以說已經是對象‘C’是堅持在‘A’是persisted.And當‘B’正在堅持它也試圖堅持‘C’再。這就是爲什麼persistenceException。

我試圖合併對象C之前我堅持對象「B」。即使如此,我得到相同的異常。

請爲我提供解決方法。

也讓我知道是否有人需要任何進一步的信息。

這裏是全堆棧跟蹤

 
    Caused by: javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: com.project.shared.entity.User 
     at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1235) 
     at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1168) 
     at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1174) 
     at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:674) 
     at com.sun.enterprise.container.common.impl.EntityManagerWrapper.persist(EntityManagerWrapper.java:258) 
     at com.project.server.bean.ExpertFacade.create(ExpertFacade.java:64) 
     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.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1052) 
     at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1124) 
     at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:5243) 
     at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:615) 
     at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:797) 
     at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:567) 
     at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doAround(SystemInterceptorProxy.java:157) 
     at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:139) 
     at sun.reflect.GeneratedMethodAccessor64.invoke(Unknown Source) 
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
     at java.lang.reflect.Method.invoke(Method.java:597) 
     at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:858) 
     at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:797) 
     at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:367) 
     at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:5215) 
     at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:5203) 
     at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:190) 
     ... 69 more

由於

+1

你能提供完整的堆棧跟蹤,好嗎? – kraftan 2010-11-30 21:36:42

+0

是的,我編輯我的問題,並粘貼堆棧跟蹤。 – Barry 2010-11-30 21:57:49

回答

4

分離實體是具有標識符的實體,但不存在於當前會話。當你嘗試保存它時,休眠抱怨。你必須把它放回到會話中。這通過entityManager.merge(..)完成。

2

我下同例外:

Exception in thread "SimpleAsyncTaskExecutor-2" javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: com.tea.domain.User 

當我修改的用戶對象,如下,我沒有得到異常:

@Override 
    public void samplePull(Application application, User user) { 

     user = userDao.findUser(user.getId()); 
     more code below..... 

     }