Barry
Barry

Reputation: 1665

Hibernate Problem ( javax.persistence.PersistenceException: org.hibernate.PersistentObjectException)

Here is my problem description.

I have 2 Objects "A" and "B"."A" has a OneToOne(cascadeType= cascadeType.All) relationship with another object "C".Even "B" has OneToOne(cascadeType = cascadeType.All) relationship with "C".

Now i need to persist the object "A".And after i persist the object "A" ,i need to persist the object "B".I can successfully persist the object "A" ,but when i persist "B" i get the exception

javax.persistence.PersistenceException: org.hibernate.PersistentObjectException

The Reason for that is already the object "C" is persisted when "A" is persisted.And when "B" is being persisted it also tries to persist "C" again.Thats why the persistenceException.

I tried to merge the object C before i persist object "B".Even then i get the same exception.

Please provide me with a work around.

Also let me know if any one needs any further info.

Here is the Full Stack Trace

   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

Thanks

Upvotes: 1

Views: 10189

Answers (2)

Arun Rajesh
Arun Rajesh

Reputation: 21

I got the same exception below:

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

When I modified the user object as below, I didn't get the exception:

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

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

      }

Upvotes: 2

Bozho
Bozho

Reputation: 597016

A Detached entity is an entity that has an identifier, but is not present in the current session. When you try to save it, hibernate complains. You have to put it back in the session. This is done via entityManager.merge(..).

Upvotes: 4

Related Questions