1 Reply Latest reply on Apr 4, 2007 2:30 AM by rabbiaqaswar

    How do you remove a detached object?

    suneetshah2000

      Hello,

      I am trying to delete a record and get the exception below which says that I am trying to delete a detached object. How can I delete a record?

      java.lang.IllegalArgumentException: Removing a detached instance common.service.Service#203
       at org.hibernate.ejb.event.EJB3DeleteEventListener.performDetachedEntityDeletionCheck(EJB3DeleteEventListener.java:45)
       at org.hibernate.event.def.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:86)
       at org.hibernate.event.def.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:52)
       at org.hibernate.impl.SessionImpl.fireDelete(SessionImpl.java:766)
       at org.hibernate.impl.SessionImpl.delete(SessionImpl.java:744)
       at org.hibernate.ejb.AbstractEntityManagerImpl.remove(AbstractEntityManagerImpl.java:246)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
       at java.lang.reflect.Method.invoke(Unknown Source)
       at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:176)
       at $Proxy19.remove(Unknown Source)
       at diamelle.common.service.ServiceDAOBean.remove(ServiceDAOBean.java:54)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
       at java.lang.reflect.Method.invoke(Unknown Source)
       at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:299)
       at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:172)
       at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:139)
       at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:107)
       at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:161)
       at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
       at $Proxy20.remove(Unknown Source)
       at diamelle.common.service.ServiceMgr.removeService(ServiceMgr.java:86)
       at diamelle.common.service.ServiceMgr$$FastClassByCGLIB$$28b3d4a2.invoke(<generated>)
       at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)
       at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:693)
       at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:139)
       at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:107)
       at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:161)
       at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:628)
       at diamelle.common.service.ServiceMgr$$EnhancerByCGLIB$$245334f0.removeService(<generated>)
       at test.diamelle.common.service.ServiceMgrTest.testRemoveService(ServiceMgrTest.java:65)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
       at java.lang.reflect.Method.invoke(Unknown Source)
       at junit.framework.TestCase.runTest(TestCase.java:164)
       at junit.framework.TestCase.runBare(TestCase.java:130)
       at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:69)
       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.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
       at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
       at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
       at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
       at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
       at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)



      Unit test Code:

      @Test
       public void testRemoveService() {
       System.out.println("Service Value = " + service);
       mgr.removeService(service);
       Service srvTest = mgr.getService(service.getServiceId());
       assertNull(srvTest);
       }
      


      RemoveService:

      public void removeService(Service serv) {
       if (serv == null)
       throw new NullPointerException("Service object is null");
       if (serv.getServiceId() == null)
       throw new NullPointerException("Service id is null");
      
       serviceDao.remove(serv);
       }



      DAO Code:

      @Transactional( propagation = Propagation.REQUIRED, readOnly = false )
       public void remove(Service persistentInstance) {
       log.debug("removing Service instance");
       try {
       entityManager.remove(persistentInstance);
      
       log.debug("remove successful");
       } catch (RuntimeException re) {
       log.error("remove failed", re);
       throw re;
       }