cannot perform operation delete on detached object

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

cannot perform operation delete on detached object

Alexander Saint Croix-2
Heya.

While working with detached entities from a Stateless session bean that was
using a transactional persistence context, I came across the following error
while calling entityManager.remove(foo):

-------------------------------------------------------------------------------

> Test set: org.eremite.corm.party.PersistenceTest
>
> -------------------------------------------------------------------------------
> Tests run: 29, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 7.706 sec
> <<< FAILURE!
> testAddressCRUD(org.eremite.corm.party.PersistenceTest)  Time elapsed:
> 5.062 sec  <<< ERROR!
> javax.ejb.EJBException: The bean encountered a non-application exception.;
> nested exception is:
>     <openjpa-1.0.1-r420667:592145 nonfatal user error>
> org.apache.openjpa.persistence.ArgumentException: You cannot perform
> operation delete on detached object "
> org.apache.openjpa.enhance.org$eremite$corm$party$address$Address$pcsubclass@515a03".
> This operation only applies to managed objects.
> FailedObject:
> org.apache.openjpa.enhance.org$eremite$corm$party$address$Address$pcsubclass@515a03
>     at org.apache.openejb.core.ivm.BaseEjbProxyHandler.convertException(
> BaseEjbProxyHandler.java:366)
>     at org.apache.openejb.core.ivm.BaseEjbProxyHandler.invoke(
> BaseEjbProxyHandler.java:323)
>     at org.apache.openejb.util.proxy.Jdk13InvocationHandler.invoke(
> Jdk13InvocationHandler.java:49)
>     at $Proxy19.remove(Unknown Source)
>     at org.eremite.corm.party.PersistenceTest.testAddressCRUD(
> PersistenceTest.java:86)
>     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:585)
>     at junit.framework.TestCase.runTest(TestCase.java:164)
>     at junit.framework.TestCase.runBare(TestCase.java:130)
>     at junit.framework.TestResult$1.protect(TestResult.java:110)
>     at junit.framework.TestResult.runProtected(TestResult.java:128)
>     at junit.framework.TestResult.run(TestResult.java:113)
>     at junit.framework.TestCase.run(TestCase.java:120)
>     at junit.framework.TestSuite.runTest(TestSuite.java:228)
>     at junit.framework.TestSuite.run(TestSuite.java:223)
>     at org.junit.internal.runners.OldTestClassRunner.run(
> OldTestClassRunner.java:35)
>     at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(
> JUnit4TestSet.java:62)
>     at
> org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(
> AbstractDirectoryTestSuite.java:138)
>     at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(
> AbstractDirectoryTestSuite.java:163)
>     at org.apache.maven.surefire.Surefire.run(Surefire.java:84)
>     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:585)
>     at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(
> SurefireBooter.java:244)
>     at org.apache.maven.surefire.booter.SurefireBooter.main(
> SurefireBooter.java:814)
> Caused by: <openjpa-1.0.1-r420667:592145 nonfatal user error>
> org.apache.openjpa.persistence.ArgumentException: You cannot perform
> operation delete on detached object "
> org.apache.openjpa.enhance.org$eremite$corm$party$address$Address$pcsubclass@515a03".
> This operation only applies to managed objects.
> FailedObject:
> org.apache.openjpa.enhance.org$eremite$corm$party$address$Address$pcsubclass@515a03
>     at org.apache.openjpa.kernel.BrokerImpl.newDetachedException(
> BrokerImpl.java:2529)
>     at org.apache.openjpa.kernel.BrokerImpl.delete(BrokerImpl.java:2520)
>     at org.apache.openjpa.kernel.BrokerImpl.delete(BrokerImpl.java:2482)
>     at org.apache.openjpa.kernel.DelegatingBroker.delete(
> DelegatingBroker.java:1030)
>     at org.apache.openjpa.persistence.EntityManagerImpl.remove(
> EntityManagerImpl.java:555)
>     at org.apache.openejb.persistence.JtaEntityManager.remove(
> JtaEntityManager.java:107)
>     at org.eremite.corm.party.BeanManagerImpl.remove(BeanManagerImpl.java
> :40)
>     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:585)
>     at
> org.apache.openejb.core.interceptor.ReflectionInvocationContext$Invocation.invoke
> (ReflectionInvocationContext.java:146)
>     at
> org.apache.openejb.core.interceptor.ReflectionInvocationContext.proceed(
> ReflectionInvocationContext.java:129)
>     at org.apache.openejb.core.interceptor.InterceptorStack.invoke(
> InterceptorStack.java:67)
>     at org.apache.openejb.core.stateless.StatelessContainer._invoke(
> StatelessContainer.java:210)
>     at org.apache.openejb.core.stateless.StatelessContainer._invoke(
> StatelessContainer.java:188)
>     at org.apache.openejb.core.stateless.StatelessContainer.invoke(
> StatelessContainer.java:165)
>     at org.apache.openejb.core.ivm.EjbObjectProxyHandler.businessMethod(
> EjbObjectProxyHandler.java:217)
>     at org.apache.openejb.core.ivm.EjbObjectProxyHandler._invoke(
> EjbObjectProxyHandler.java:77)
>     at org.apache.openejb.core.ivm.BaseEjbProxyHandler.invoke(
> BaseEjbProxyHandler.java:321)
>     ... 26 more
>

David showed me how to resolve this.  I was calling:

    public void remove(Archetype ... archs) {
        for(Archetype a : archs) {
            entityManager.remove(a);
        }
    }

But I needed to reattach the detached entity bean.  So, I changed it to:

    public void remove(Archetype ... archs) {
        for(Archetype a : archs) {
            entityManager.remove(entityManager.merge(a));
        }
    }

Problem solved (for posterity).  Thanks, David!
--
Alex