title: InvalidDataAccessApiUsageException after switchin from sessionFactory to EntityManager
body: <p>there were an existing application, which everything worked fine. But for various reasons I had to switch from <code>sessionFactory "org.springframework.orm.hibernate3.LocalSessionFactoryBean"</code> to the <code>entityManagerFactory "org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"</code>. Now I´m getting an <code>org.springframework.dao.InvalidDataAccessApiUsageException: Removing a detached instance server.model.instance.ComponentInstance</code> at a <code>junit-integration.Test</code>.
  After a five or six hours of fruitless googling, I decided to write my issue here. I would be thankful and It would be great for any help.</p>
  
  <p>This is the junit test code, and the code which is mentioned at the stack trace:</p>
  
  <pre><code>public void deleteComponentInstance ( ComponentInstance instanceToDelete ) {
      setComponentInstanceForeignKeyToNull ( instanceToDelete );
      deleteAndFlush ( instanceToDelete );
  }
  </code></pre>
  
  <p>the Method deleteAndFlush</p>
  
  <pre><code>public &lt;P&gt; void deleteAndFlush ( P persistentObject ) {
      Session session = sessionFactory.getCurrentSession ();
      session.delete ( persistentObject );
      session.flush ();
  }
  </code></pre>
  
  <p>the Method setComponentInstanceForeignKeyToNull</p>
  
  <pre><code>public void setComponentInstanceForeignKeyToNull ( Class&lt;?&gt; clazz, String attribute, Object value ) {
      StringBuilder hqlQuery = new StringBuilder ()
          .append ( "update " )
          .append ( clazz.getSimpleName () )
          .append ( " entity set entity." ).append ( attribute )
          .append ( " = null where entity." ).append ( attribute )
          .append ( " = :actualValue" );
      Session currentSession = this.sessionFactory.getCurrentSession ();
      currentSession.createQuery ( hqlQuery.toString () )
          .setEntity ( "actualValue", value )
          .executeUpdate ();        
  }
  </code></pre>
  
  <p>this is my "beans.xml" after switching to the entityManager</p>
  
  <pre><code>    &lt;bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
        &lt;property name="dataSource" ref="dataSource" /&gt;
        &lt;property name="persistenceUnitName" value="spring-jpa" /&gt;
        &lt;property name="jpaVendorAdapter"&gt;
           &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"&gt;
              &lt;property name="showSql" value="false" /&gt;
                  &lt;property name="database" value="HSQL" /&gt;
           &lt;/bean&gt;
        &lt;/property&gt;
     &lt;/bean&gt;
  
     &lt;bean id="sessionFactory" factory-bean="entityManagerFactory" factory-method="getSessionFactory" /&gt;
  
     &lt;tx:annotation-driven transaction-manager="transactionManager" /&gt;
     &lt;bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
        &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
     &lt;/bean&gt;
  </code></pre>
  
  <p>this is my beans.xml befor the switch:</p>
  
  <pre><code>&lt;bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;
    &lt;property name="dataSource" ref="dataSource" /&gt;
    &lt;property name="mappingLocations" ref="hibernateMappingFiles" /&gt;
    &lt;property name="hibernateProperties" ref="hibernateProperties" /&gt;
  </code></pre>
  
  <p></p>
  
  <pre><code>    &lt;tx:annotation-driven transaction-manager="transactionManager" /&gt;
      &lt;bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
    &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
  </code></pre>
  
  <p></p>
  
  <p>
        
           
        
     </p>
  
  <p>my new needed persistence.xml for the EntityManager:</p>
  
  <pre><code>&lt;persistence-unit name="spring-jpa"&gt;
    &lt;properties&gt;
          &lt;property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" /&gt;
       &lt;property name="hibernate.show_sql" value="false" /&gt;
       &lt;property name="hibernate.format_sql" value="false" /&gt;
       &lt;property name="hibernate.hbm2ddl.auto" value="none" /&gt;
       &lt;property name="javax.persistence.validation.mode" value="none" /&gt;
       &lt;property name="hibernate.jdbc.use_streams_for_binary"
          value="true" /&gt;
       &lt;property name="hibernate.jdbc.fetch_size" value="100" /&gt;
       &lt;property name="hibernate.current_session_context_class" value="org.springframework.orm.hibernate3.SpringSessionContext" /&gt;
    &lt;/properties&gt;
  </code></pre>
  
  <p></p>
  
  <p>the Stack Trace/Failure Trace:</p>
  
  <pre><code>    2011-11-30 09:41:01,890 [org.springframework.scheduling.concurrent.ThreadPoolExecutorFactoryBean#4929b0e1-1] INFO  server.interpreter.ast.ExecNativeOperator:893 - ExecNativeOperator SuccessCriteria RC satisfied
  2011-11-30 09:41:01,890 [org.springframework.scheduling.concurrent.ThreadPoolExecutorFactoryBean#4929b0e1-1] INFO  server.interpreter.ast.ExecNativeOperator:481 - ExecNativeOperator executeProcess() finished successfully.
  2011-11-30 09:41:01,900 [org.springframework.scheduling.concurrent.ThreadPoolExecutorFactoryBean#4929b0e1-1] INFO  server.locking.DeadlockDetectionLock:324 - Successfully released EXCLUSIVE server.locking.DeadlockDetectionLock@511ee8dd.
  2011-11-30 09:41:01,924 [org.springframework.scheduling.concurrent.ThreadPoolExecutorFactoryBean#4929b0e1-1] ERROR server.interpreter.step.StepTemplate:162 - Interpreter step aborted.
  org.springframework.dao.InvalidDataAccessApiUsageException: Removing a detached instance server.model.instance.ComponentInstance#ComponentInstanceId[id=140]; nested exception is java.lang.IllegalArgumentException: Removing a detached instance server.model.instance.ComponentInstance#ComponentInstanceId[id=140]
     at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:291)
     at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:125)
     at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:403)
     at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:58)
     at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
     at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:163)
     at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
     at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
     at $Proxy65.deleteAndFlush(Unknown Source)
     at server.component.control.impl.ComponentInstanceServiceImpl.deleteComponentInstance(ComponentInstanceServiceImpl.java:66)
     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.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318)
     at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
     at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
     at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
     at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
     at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
     at $Proxy93.deleteComponentInstance(Unknown Source)
     at server.interpreter.ast.UninstallOperator$2.evaluateStep(UninstallOperator.java:143)
     at server.interpreter.ast.UninstallOperator$2.evaluateStep(UninstallOperator.java:1)
     at server.interpreter.step.StepTemplate$StepExceptionHandler.performProtectedAction(StepTemplate.java:133)
     at server.interpreter.step.StepTemplate.protocolStep(StepTemplate.java:86)
     at server.interpreter.ast.UninstallOperator.doEvaluate(UninstallOperator.java:152)
     at server.interpreter.AbstractEvaluable.evaluate(AbstractEvaluable.java:57)
     at server.interpreter.AbstractEvaluable.nullSafeEvaluate(AbstractEvaluable.java:123)
     at server.interpreter.ast.SemicolonOperator.doEvaluate(SemicolonOperator.java:42)
     at server.interpreter.AbstractEvaluable.evaluate(AbstractEvaluable.java:57)
     at server.interpreter.AbstractEvaluable.nullSafeEvaluate(AbstractEvaluable.java:123)
     at server.interpreter.ast.BlockOperator.doEvaluate(BlockOperator.java:41)
     at server.interpreter.AbstractEvaluable.evaluate(AbstractEvaluable.java:57)
     at server.interpreter.ast.IfOperator$3.evaluateStep(IfOperator.java:128)
     at server.interpreter.step.StepTemplate$StepExceptionHandler.performProtectedAction(StepTemplate.java:133)
     at server.interpreter.step.StepTemplate.protocolStep(StepTemplate.java:86)
     at server.interpreter.ast.IfOperator.evaluateAndProtocolBlock(IfOperator.java:131)
     at server.interpreter.ast.IfOperator.access$2(IfOperator.java:119)
     at server.interpreter.ast.IfOperator$2.evaluateStep(IfOperator.java:68)
     at server.interpreter.ast.IfOperator$2.evaluateStep(IfOperator.java:1)
     at server.interpreter.step.StepTemplate$StepExceptionHandler.performProtectedAction(StepTemplate.java:133)
     at server.interpreter.step.StepTemplate.protocolStep(StepTemplate.java:86)
         at server.interpreter.ast.IfOperator.doEvaluate(IfOperator.java:76)
     at server.interpreter.AbstractEvaluable.evaluate(AbstractEvaluable.java:57)
     at server.security.SecurityContextInheritingCallable.call(SecurityContextInheritingCallable.java:47)
     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
     at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
     at java.lang.Thread.run(Thread.java:662)
  Caused by: java.lang.IllegalArgumentException: Removing a detached instance server.model.instance.ComponentInstance#ComponentInstanceId[id=140]
     at org.hibernate.ejb.event.EJB3DeleteEventListener.performDetachedEntityDeletionCheck(EJB3DeleteEventListener.java:65)
     at org.hibernate.event.def.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:108)
     at org.hibernate.event.def.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:74)
     at org.hibernate.impl.SessionImpl.fireDelete(SessionImpl.java:948)
     at org.hibernate.impl.SessionImpl.delete(SessionImpl.java:926)
     at server.dao.hibernate.HibernateGenericDao.deleteAndFlush(HibernateGenericDao.java:32)
     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.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318)
     at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
     at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
     at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:155)
     ... 78 more
  2    011-11-30 09:41:01,951 [org.springframework.scheduling.concurrent.ThreadPoolExecutorFactoryBean#4929b0e1-1] WARN  server.interpreter.step.StepTemplate:154 - Interpreter step aborted.
  server.process.control.StepAbortionAlreadyProtocolledException: org.springframework.dao.InvalidDataAccessApiUsageException: Removing a detached instance server.model.instance.ComponentInstance#ComponentInstanceId[id=140]; nested exception is java.lang.IllegalArgumentException: Removing a detached instance server.model.instance.ComponentInstance#ComponentInstanceId[id=140]
     at server.interpreter.step.StepTemplate$StepExceptionHandler.performProtectedAction(StepTemplate.java:164)
     at server.interpreter.ast.IfOperator.doEvaluate(IfOperator.java:76)
     at server.interpreter.AbstractEvaluable.evaluate(AbstractEvaluable.java:57)
     at server.interpreter.AbstractEvaluable.nullSafeEvaluate(AbstractEvaluable.java:123)
     at server.interpreter.ast.SemicolonOperator.doEvaluate(SemicolonOperator.java:42)
     at server.interpreter.AbstractEvaluable.evaluate(AbstractEvaluable.java:57)
     at server.interpreter.AbstractEvaluable.nullSafeEvaluate(AbstractEvaluable.java:123)
     at server.interpreter.ast.SemicolonOperator.doEvaluate(SemicolonOperator.java:43)
     at server.interpreter.AbstractEvaluable.evaluate(AbstractEvaluable.java:57)
     at server.interpreter.AbstractEvaluable.nullSafeEvaluate(AbstractEvaluable.java:123)
     at server.interpreter.ast.SemicolonOperator.doEvaluate(SemicolonOperator.java:43)
     at server.interpreter.AbstractEvaluable.evaluate(AbstractEvaluable.java:57)
     at server.interpreter.AbstractEvaluable.nullSafeEvaluate(AbstractEvaluable.java:123)
     at server.interpreter.ast.BlockOperator.doEvaluate(BlockOperator.java:41)
     at server.interpreter.AbstractEvaluable.evaluate(AbstractEvaluable.java:57)
     at server.interpreter.ast.HostLockingOperator$2$2.doWhileLocked(HostLockingOperator.java:210)
     at server.host.control.impl.HostLockingServiceImpl$1.doWhileLocked(HostLockingServiceImpl.java:161)
     at server.host.control.impl.HostLockingServiceImpl.doLockAndExecute(HostLockingServiceImpl.java:199)
     at server.host.control.impl.HostLockingServiceImpl.lockAndExecute(HostLockingServiceImpl.java:148)
     at server.interpreter.ast.HostLockingOperator$2.evaluateStep(HostLockingOperator.java:199)
     at server.interpreter.ast.HostLockingOperator$2.evaluateStep(HostLockingOperator.java:1)
     at server.interpreter.step.StepTemplate$StepExceptionHandler.performProtectedAction(StepTemplate.java:133)
     at server.interpreter.step.StepTemplate.protocolStep(StepTemplate.java:86)
     at server.interpreter.ast.HostLockingOperator.doEvaluate(HostLockingOperator.java:229)
     at server.interpreter.AbstractEvaluable.evaluate(AbstractEvaluable.java:57)
     at server.interpreter.AbstractEvaluable.nullSafeEvaluate(AbstractEvaluable.java:123)
     at server.interpreter.ast.MethodReturnOperator.doEvaluate(MethodReturnOperator.java:44)
     at server.interpreter.AbstractEvaluable.evaluate(AbstractEvaluable.java:57)
     at server.interpreter.ast.PlanOperator.doEvaluate(PlanOperator.java:54)
     at server.interpreter.AbstractEvaluable.evaluate(AbstractEvaluable.java:57)
     at server.model.boundary.impl.ModelExecutionServiceImpl$6.doExecute(ModelExecutionServiceImpl.java:483)
     at server.job.control.ProtocolSubJob.execute(ProtocolSubJob.java:86)
     at server.job.control.impl.JobExecutorServiceImpl$JobCallable.call(JobExecutorServiceImpl.java:497)
     at server.job.control.impl.JobExecutorServiceImpl$2.call(JobExecutorServiceImpl.java:249)
     at server.security.SecurityContextInheritingCallable.call(SecurityContextInheritingCallable.java:47)
     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
     at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
     at java.lang.Thread.run(Thread.java:662)
  Caused by: org.springframework.dao.InvalidDataAccessApiUsageException: Removing a detached instance server.model.instance.ComponentInstance#ComponentInstanceId[id=140]; nested exception is java.lang.IllegalArgumentException: Removing a detached instance server.model.instance.ComponentInstance#ComponentInstanceId[id=140]
     at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:291)
     at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:125)
     at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:403)
     at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:58)
     at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
     at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:163)
     at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
     at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
     at $Proxy65.deleteAndFlush(Unknown Source)
     at server.component.control.impl.ComponentInstanceServiceImpl.deleteComponentInstance(ComponentInstanceServiceImpl.java:66)
     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.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318)
     at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
     at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
     at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
     at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
     at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
     at $Proxy93.deleteComponentInstance(Unknown Source)
     at server.interpreter.ast.UninstallOperator$2.evaluateStep(UninstallOperator.java:143)
     at server.interpreter.ast.UninstallOperator$2.evaluateStep(UninstallOperator.java:1)
     at server.interpreter.step.StepTemplate$StepExceptionHandler.performProtectedAction(StepTemplate.java:133)
     ... 60 more
  Caused by: java.lang.IllegalArgumentException: Removing a detached instance server.model.instance.ComponentInstance#ComponentInstanceId[id=140]
     at org.hibernate.ejb.event.EJB3DeleteEventListener.performDetachedEntityDeletionCheck(EJB3DeleteEventListener.java:65)
     at org.hibernate.event.def.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:108)
     at org.hibernate.event.def.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:74)
     at org.hibernate.impl.SessionImpl.fireDelete(SessionImpl.java:948)
     at org.hibernate.impl.SessionImpl.delete(SessionImpl.java:926)
     at server.dao.hibernate.HibernateGenericDao.deleteAndFlush(HibernateGenericDao.java:32)
     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.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318)
     at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
     at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
     at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:155)
     ... 78 more
  </code></pre>