1 Reply Latest reply on Apr 29, 2008 12:27 PM by JB Arnoux

    Integration Test: object references an unsaved transient instance

    Adrien Orsier Newbie

      Hi,


      I'm trying to run several tests in my application, and I'm facing what I believe to be the same bug in several tests.


      First of all, everything is working fine in the application, I just have some troubles with the test and the same code.


      Basically, a object references an unsaved transient instance exception is thrown where it shouldn't be for odd reasons.
      Example:


      Everytime I perform a login in my test, I always have this exception on the auto performed logout (which is actually performing some database changes as you'll see below).
      Here's my test data:


      insert into Language (id, name, code,) values (0, 'English','en')
      insert into Users(id, username, password,language_id) values (0,'admin' , 'admin', 0)



      Nothing too fancy.


      When I login, I create an Event, which has a @OneToOne link to a User.


      userEvent = new Event();
      userEvent.setUser(currentUser);
                     
      entityManager.persist(userEvent);


      It's working well here too.


      Then, the auto-performed logout:


      @Observer("org.jboss.seam.preDestroyContext.SESSION")
           public void logout() {
                if (userEvent != null) {
                     try {
                          userEvent.end();
                          entityManager.merge(userEvent);
                     } catch (Exception e) {
                          log.error(e,e.getMessage());
                     }
                }


      It's right there, on the entityManager.merge, that I have this strange exception:


         [testng] java.lang.IllegalStateException: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.lrb.metabook.entities.User
         [testng]      at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:627)
         [testng]      at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:237)
         [testng]      at org.jboss.seam.persistence.EntityManagerProxy.merge(EntityManagerProxy.java:130)
         [testng]      at org.hibernate.search.jpa.impl.FullTextEntityManagerImpl.merge(FullTextEntityManagerImpl.java:86)
         [testng]      at org.jboss.seam.persistence.EntityManagerProxy.merge(EntityManagerProxy.java:130)
         [testng]      at com.lrb.metabook.AuthenticatorImpl.logout(AuthenticatorImpl.java:172)
         [testng]      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
         [testng]      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
         [testng]      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
         [testng]      at java.lang.reflect.Method.invoke(Method.java:585)
         [testng]      at org.jboss.aop.joinpoint.MethodInvocation.invokeTarget(MethodInvocation.java:121)
         [testng]      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:110)
         [testng]      at org.jboss.ejb3.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:166)
         [testng]      at org.jboss.seam.intercept.EJBInvocationContext.proceed(EJBInvocationContext.java:44)
         [testng]      at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:56)
         [testng]      at org.jboss.seam.transaction.RollbackInterceptor.aroundInvoke(RollbackInterceptor.java:31)
         [testng]      at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
         [testng]      at org.jboss.seam.core.BijectionInterceptor.aroundInvoke(BijectionInterceptor.java:46)
         [testng]      at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
         [testng]      at org.jboss.seam.bpm.BusinessProcessInterceptor.aroundInvoke(BusinessProcessInterceptor.java:49)
         [testng]      at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
         [testng]      at org.jboss.seam.core.EventInterceptor.aroundInvoke(EventInterceptor.java:51)
         [testng]      at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
         [testng]      at org.jboss.seam.core.MethodContextInterceptor.aroundInvoke(MethodContextInterceptor.java:42)
         [testng]      at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
         [testng]      at org.jboss.seam.persistence.EntityManagerProxyInterceptor.aroundInvoke(EntityManagerProxyInterceptor.java:26)
         [testng]      at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
         [testng]      at org.jboss.seam.persistence.HibernateSessionProxyInterceptor.aroundInvoke(HibernateSessionProxyInterceptor.java:27)
         [testng]      at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
         [testng]      at org.jboss.seam.intercept.RootInterceptor.invoke(RootInterceptor.java:107)
         [testng]      at org.jboss.seam.intercept.SessionBeanInterceptor.aroundInvoke(SessionBeanInterceptor.java:50)
         [testng]      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
         [testng]      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
         [testng]      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
         [testng]      at java.lang.reflect.Method.invoke(Method.java:585)
         [testng]      at org.jboss.ejb3.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:118)
         [testng]      at org.jboss.ejb3.interceptor.EJB3InterceptorsInterceptor.invoke(EJB3InterceptorsInterceptor.java:63)
         [testng]      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
         [testng]      at org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invoke(TransactionScopedEntityManagerInterceptor.java:54)
         [testng]      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
         [testng]      at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:47)
         [testng]      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
         [testng]      at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:79)
         [testng]      at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:193)
         [testng]      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
         [testng]      at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:76)
         [testng]      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
         [testng]      at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:64)
         [testng]      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
         [testng]      at org.jboss.ejb3.tx.NullInterceptor.invoke(NullInterceptor.java:42)
         [testng]      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
         [testng]      at org.jboss.ejb3.security.Ejb3AuthenticationInterceptorv2.invoke(Ejb3AuthenticationInterceptorv2.java:166)
         [testng]      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
         [testng]      at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:46)
         [testng]      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
         [testng]      at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:106)
         [testng]      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
         [testng]      at org.jboss.ejb3.stateless.StatelessContainer.localInvoke(StatelessContainer.java:249)
         [testng]      at org.jboss.ejb3.stateless.StatelessContainer.localInvoke(StatelessContainer.java:214)
         [testng]      at org.jboss.ejb3.stateless.StatelessLocalProxy.invoke(StatelessLocalProxy.java:86)
         [testng]      at $Proxy115.logout(Unknown Source)
         [testng]      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
         [testng]      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
         [testng]      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
         [testng]      at java.lang.reflect.Method.invoke(Method.java:585)
         [testng]      at org.jboss.seam.util.Reflections.invoke(Reflections.java:21)
         [testng]      at org.jboss.seam.intercept.RootInvocationContext.proceed(RootInvocationContext.java:31)
         [testng]      at org.jboss.seam.intercept.ClientSideInterceptor$1.proceed(ClientSideInterceptor.java:76)
         [testng]      at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:56)
         [testng]      at org.jboss.seam.intercept.RootInterceptor.invoke(RootInterceptor.java:107)
         [testng]      at org.jboss.seam.intercept.ClientSideInterceptor.invoke(ClientSideInterceptor.java:54)
         [testng]      at org.javassist.tmp.java.lang.Object_$$_javassist_8.logout(Object_$$_javassist_8.java)
         [testng]      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
         [testng]      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
         [testng]      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
         [testng]      at java.lang.reflect.Method.invoke(Method.java:585)
         [testng]      at org.jboss.seam.util.Reflections.invoke(Reflections.java:21)
         [testng]      at org.jboss.seam.util.Reflections.invokeAndWrap(Reflections.java:125)
         [testng]      at org.jboss.seam.Component.callComponentMethod(Component.java:2082)
         [testng]      at org.jboss.seam.core.Events.raiseEvent(Events.java:84)
         [testng]      at org.jboss.seam.contexts.Contexts.destroy(Contexts.java:236)
         [testng]      at org.jboss.seam.contexts.Lifecycle.endSession(Lifecycle.java:249)
         [testng]      at org.jboss.seam.contexts.ServletLifecycle.endSession(ServletLifecycle.java:129)
         [testng]      at org.jboss.seam.mock.BaseSeamTest.end(BaseSeamTest.java:917)
         [testng]      at org.jboss.seam.mock.SeamTest.end(SeamTest.java:35)
         [testng]      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
         [testng]      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
         [testng]      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
         [testng]      at java.lang.reflect.Method.invoke(Method.java:585)
         [testng]      at org.testng.internal.MethodHelper.invokeMethod(MethodHelper.java:604)
         [testng]      at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:394)
         [testng]      at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:142)
         [testng]      at org.testng.internal.Invoker.invokeMethod(Invoker.java:521)
         [testng]      at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:564)
         [testng]      at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:830)
         [testng]      at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
         [testng]      at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
         [testng]      at org.testng.TestRunner.runWorkers(TestRunner.java:678)
         [testng]      at org.testng.TestRunner.privateRun(TestRunner.java:624)
         [testng]      at org.testng.TestRunner.run(TestRunner.java:495)
         [testng]      at org.testng.SuiteRunner.runTest(SuiteRunner.java:300)
         [testng]      at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:295)
         [testng]      at org.testng.SuiteRunner.privateRun(SuiteRunner.java:275)
         [testng]      at org.testng.SuiteRunner.run(SuiteRunner.java:190)
         [testng]      at org.testng.TestNG.createAndRunSuiteRunners(TestNG.java:792)
         [testng]      at org.testng.TestNG.runSuitesLocally(TestNG.java:765)
         [testng]      at org.testng.TestNG.run(TestNG.java:699)
         [testng]      at org.testng.TestNG.privateMain(TestNG.java:824)
         [testng]      at org.testng.TestNG.main(TestNG.java:802)
         [testng] Caused by: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.lrb.metabook.entities.User
         [testng]      at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:219)
         [testng]      at org.hibernate.type.EntityType.getIdentifier(EntityType.java:397)
         [testng]      at org.hibernate.type.EntityType.replace(EntityType.java:253)
         [testng]      at org.hibernate.type.TypeFactory.replace(TypeFactory.java:482)
         [testng]      at org.hibernate.event.def.DefaultMergeEventListener.copyValues(DefaultMergeEventListener.java:340)
         [testng]      at org.hibernate.event.def.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:267)
         [testng]      at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:120)
         [testng]      at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:53)
         [testng]      at org.hibernate.impl.SessionImpl.fireMerge(SessionImpl.java:677)
         [testng]      at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:661)
         [testng]      at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:665)
         [testng]      at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:228)
         [testng]      ... 107 more



      I actually have no idea why it's happening, since I have this same problem on different test in my application, some being on totally different models than this ones.