1 Reply Latest reply on May 19, 2007 10:51 AM by fernando_jmt

    LazyInitializationException on SeamTest

    fernando_jmt

      Hi.

      Somebody knows how to deal with LazyInitializationException when using Seam integration test?

      I got this exception: org.hibernate.LazyInitializationException.


      The simple test:

      public class LoginTest extends SeamTest {
      
       @Test
       public void testLogin() throws Exception {
      
       new FacesRequest() {
      
       @Override
       protected void invokeApplication() {
       assert !isSessionInvalid();
       assert getValue("#{identity.loggedIn}").equals(false);
       }
      
       }.run();
      
       new FacesRequest() {
      
       @Override
       protected void updateModelValues() throws Exception {
       assert !isSessionInvalid();
       setValue("#{identity.username}", "fer");
       setValue("#{identity.password}", "fernando");
       }
      
       @Override
       protected void invokeApplication() {
       invokeMethod("#{identity.login}");
       }
      
       @Override
       protected void renderResponse() {
       assert getValue("#{identity.username}").equals("fer");
       assert !Manager.instance().isLongRunningConversation();
       assert getValue("#{identity.loggedIn}").equals(true);
       }
      
       }.run();
      
       new FacesRequest() {
      
       @Override
       protected void invokeApplication() {
       assert !isSessionInvalid();
       assert getValue("#{identity.loggedIn}").equals(true);
       }
      
       }.run();
      
       new FacesRequest() {
      
       @Override
       protected void invokeApplication() {
       assert !Manager.instance().isLongRunningConversation();
       assert !isSessionInvalid();
       invokeMethod("#{identity.logout}");
       assert Seam.isSessionInvalid();
       }
      
       @Override
       protected void renderResponse() {
       assert getValue("#{identity.loggedIn}").equals(false);
       assert Seam.isSessionInvalid();
       }
      
       }.run();
      
       }
      
      }
      



      The authenticator class:

      
      @Name("authenticator")
      public class AuthenticatorAction implements Serializable {
      
       private static final long serialVersionUID = -3938569313203918047L;
      
       @Logger
       private Log log;
      
       @In(create = true)
       private UserManager userManager;
      
      
       @In
       private Identity identity;
      
       public boolean authenticate() {
      
       try {
       User currentUser = userManager.findByUsernameAndPassword(identity.getUsername(),
       Hash.instance().hash(identity.getPassword()));
       if (currentUser.getRoles() != null) {
       for (Role role : currentUser.getRoles()) {
       identity.addRole(role.getName());
       }
       }
      
       return true;
      
       } catch (EntryNotFoundException e) {
       return false;
       }
       }
      
       p
      }
      



      The user entity (fragment)
      
      @Entity
      @Table(name = "users")
      @Name("user")
      public class User implements Serializable {
      ...
      @ManyToMany(fetch = FetchType.LAZY)
       @OrderBy("name asc")
       private List<Role> roles;
      ...
      }
      



      Any help will be appreciated.


      Thanks in advance.



      The stacktrace:

      21:39:54,515 ERROR [LazyInitializationException] failed to lazily initialize a collection of role: com.jtp.app.domain.User.roles, no session or session was closed
      org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.jtp.app.domain.User.roles, no session or session was closed
       at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)
       at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
       at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:343)
       at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
       at org.hibernate.collection.PersistentBag.iterator(PersistentBag.java:246)
       at com.jtp.app.action.AuthenticatorAction.authenticate(AuthenticatorAction.java:45)
       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.jboss.seam.util.Reflections.invoke(Reflections.java:20)
       at org.jboss.seam.intercept.RootInvocationContext.proceed(RootInvocationContext.java:61)
       at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:57)
       at org.jboss.seam.interceptors.RollbackInterceptor.aroundInvoke(RollbackInterceptor.java:34)
       at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:69)
       at org.jboss.seam.interceptors.BijectionInterceptor.aroundInvoke(BijectionInterceptor.java:47)
       at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:69)
       at org.jboss.seam.interceptors.MethodContextInterceptor.aroundInvoke(MethodContextInterceptor.java:27)
       at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:69)
       at org.jboss.seam.intercept.RootInterceptor.invoke(RootInterceptor.java:103)
       at org.jboss.seam.intercept.JavaBeanInterceptor.interceptInvocation(JavaBeanInterceptor.java:151)
       at org.jboss.seam.intercept.JavaBeanInterceptor.invoke(JavaBeanInterceptor.java:87)
       at com.jtp.app.action.AuthenticatorAction_$$_javassist_0.authenticate(AuthenticatorAction_$$_javassist_0.java)
       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 com.sun.el.parser.AstValue.invoke(AstValue.java:174)
       at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:286)
       at org.jboss.seam.util.UnifiedELMethodBinding.invoke(UnifiedELMethodBinding.java:71)
       at org.jboss.seam.actionparam.ActionParamBindingHelper.invokeTheExpression(ActionParamBindingHelper.java:58)
       at org.jboss.seam.actionparam.ActionParamMethodBinding.invoke(ActionParamMethodBinding.java:75)
       at org.jboss.seam.core.Expressions$2.invoke(Expressions.java:148)
       at org.jboss.seam.security.jaas.SeamLoginModule.login(SeamLoginModule.java:104)
       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 javax.security.auth.login.LoginContext.invoke(LoginContext.java:769)
       at javax.security.auth.login.LoginContext.access$000(LoginContext.java:186)
       at javax.security.auth.login.LoginContext$5.run(LoginContext.java:706)
       at java.security.AccessController.doPrivileged(Native Method)
       at javax.security.auth.login.LoginContext.invokeCreatorPriv(LoginContext.java:703)
       at javax.security.auth.login.LoginContext.login(LoginContext.java:575)
       at org.jboss.seam.security.Identity.authenticate(Identity.java:247)
       at org.jboss.seam.security.Identity.authenticate(Identity.java:240)
       at org.jboss.seam.security.Identity.login(Identity.java:170)
       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 com.sun.el.parser.AstValue.invoke(AstValue.java:174)
       at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:286)
       at org.jboss.seam.util.UnifiedELMethodBinding.invoke(UnifiedELMethodBinding.java:71)
       at org.jboss.seam.actionparam.ActionParamBindingHelper.invokeTheExpression(ActionParamBindingHelper.java:58)
       at org.jboss.seam.actionparam.ActionParamMethodBinding.invoke(ActionParamMethodBinding.java:75)
       at org.jboss.seam.actionparam.ActionParamBindingHelper.invokeTheExpression(ActionParamBindingHelper.java:58)
       at org.jboss.seam.actionparam.ActionParamMethodBinding.invoke(ActionParamMethodBinding.java:75)
       at org.jboss.seam.mock.SeamTest$Request.invokeMethod(SeamTest.java:401)
       at com.jtp.app.test.LoginTest$2.invokeApplication(LoginTest.java:40)
       at org.jboss.seam.mock.SeamTest$Request.run(SeamTest.java:489)
       at com.jtp.app.test.LoginTest.testLogin(LoginTest.java:29)
       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.testng.internal.MethodHelper.invokeMethod(MethodHelper.java:645)
       at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:479)
       at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:715)
       at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
       at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
       at org.testng.TestRunner.runWorkers(TestRunner.java:673)
       at org.testng.TestRunner.privateRun(TestRunner.java:620)
       at org.testng.TestRunner.run(TestRunner.java:480)
       at org.testng.SuiteRunner.runTest(SuiteRunner.java:278)
       at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:273)
       at org.testng.SuiteRunner.privateRun(SuiteRunner.java:253)
       at org.testng.SuiteRunner.run(SuiteRunner.java:168)
       at org.testng.TestNG.createAndRunSuiteRunners(TestNG.java:987)
       at org.testng.TestNG.runSuitesLocally(TestNG.java:951)
       at org.testng.TestNG.run(TestNG.java:719)
       at org.testng.TestNG.privateMain(TestNG.java:1019)
       at org.testng.TestNG.main(TestNG.java:997)
      21:39:54,531 ERROR [SeamLoginModule] Error invoking login method
      javax.el.ELException: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.jtp.app.domain.User.roles, no session or session was closed
       at com.sun.el.parser.AstValue.invoke(AstValue.java:178)
       at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:286)
       at org.jboss.seam.util.UnifiedELMethodBinding.invoke(UnifiedELMethodBinding.java:71)
       at org.jboss.seam.actionparam.ActionParamBindingHelper.invokeTheExpression(ActionParamBindingHelper.java:58)
       at org.jboss.seam.actionparam.ActionParamMethodBinding.invoke(ActionParamMethodBinding.java:75)
       at org.jboss.seam.core.Expressions$2.invoke(Expressions.java:148)
       at org.jboss.seam.security.jaas.SeamLoginModule.login(SeamLoginModule.java:104)
       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 javax.security.auth.login.LoginContext.invoke(LoginContext.java:769)
       at javax.security.auth.login.LoginContext.access$000(LoginContext.java:186)
       at javax.security.auth.login.LoginContext$5.run(LoginContext.java:706)
       at java.security.AccessController.doPrivileged(Native Method)
       at javax.security.auth.login.LoginContext.invokeCreatorPriv(LoginContext.java:703)
       at javax.security.auth.login.LoginContext.login(LoginContext.java:575)
       at org.jboss.seam.security.Identity.authenticate(Identity.java:247)
       at org.jboss.seam.security.Identity.authenticate(Identity.java:240)
       at org.jboss.seam.security.Identity.login(Identity.java:170)
       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 com.sun.el.parser.AstValue.invoke(AstValue.java:174)
       at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:286)
       at org.jboss.seam.util.UnifiedELMethodBinding.invoke(UnifiedELMethodBinding.java:71)
       at org.jboss.seam.actionparam.ActionParamBindingHelper.invokeTheExpression(ActionParamBindingHelper.java:58)
       at org.jboss.seam.actionparam.ActionParamMethodBinding.invoke(ActionParamMethodBinding.java:75)
       at org.jboss.seam.actionparam.ActionParamBindingHelper.invokeTheExpression(ActionParamBindingHelper.java:58)
       at org.jboss.seam.actionparam.ActionParamMethodBinding.invoke(ActionParamMethodBinding.java:75)
       at org.jboss.seam.mock.SeamTest$Request.invokeMethod(SeamTest.java:401)
       at com.jtp.app.test.LoginTest$2.invokeApplication(LoginTest.java:40)
       at org.jboss.seam.mock.SeamTest$Request.run(SeamTest.java:489)
       at com.jtp.app.test.LoginTest.testLogin(LoginTest.java:29)
       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.testng.internal.MethodHelper.invokeMethod(MethodHelper.java:645)
       at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:479)
       at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:715)
       at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
       at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
       at org.testng.TestRunner.runWorkers(TestRunner.java:673)
       at org.testng.TestRunner.privateRun(TestRunner.java:620)
       at org.testng.TestRunner.run(TestRunner.java:480)
       at org.testng.SuiteRunner.runTest(SuiteRunner.java:278)
       at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:273)
       at org.testng.SuiteRunner.privateRun(SuiteRunner.java:253)
       at org.testng.SuiteRunner.run(SuiteRunner.java:168)
       at org.testng.TestNG.createAndRunSuiteRunners(TestNG.java:987)
       at org.testng.TestNG.runSuitesLocally(TestNG.java:951)
       at org.testng.TestNG.run(TestNG.java:719)
       at org.testng.TestNG.privateMain(TestNG.java:1019)
       at org.testng.TestNG.main(TestNG.java:997)
      Caused by: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.jtp.app.domain.User.roles, no session or session was closed
       at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)
       at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
       at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:343)
       at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
       at org.hibernate.collection.PersistentBag.iterator(PersistentBag.java:246)
       at com.jtp.app.action.AuthenticatorAction.authenticate(AuthenticatorAction.java:45)
       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.jboss.seam.util.Reflections.invoke(Reflections.java:20)
       at org.jboss.seam.intercept.RootInvocationContext.proceed(RootInvocationContext.java:61)
       at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:57)
       at org.jboss.seam.interceptors.RollbackInterceptor.aroundInvoke(RollbackInterceptor.java:34)
       at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:69)
       at org.jboss.seam.interceptors.BijectionInterceptor.aroundInvoke(BijectionInterceptor.java:47)
       at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:69)
       at org.jboss.seam.interceptors.MethodContextInterceptor.aroundInvoke(MethodContextInterceptor.java:27)
       at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:69)
       at org.jboss.seam.intercept.RootInterceptor.invoke(RootInterceptor.java:103)
       at org.jboss.seam.intercept.JavaBeanInterceptor.interceptInvocation(JavaBeanInterceptor.java:151)
       at org.jboss.seam.intercept.JavaBeanInterceptor.invoke(JavaBeanInterceptor.java:87)
       at com.jtp.app.action.AuthenticatorAction_$$_javassist_0.authenticate(AuthenticatorAction_$$_javassist_0.java)
       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 com.sun.el.parser.AstValue.invoke(AstValue.java:174)
       ... 55 more
      21:39:54,593 INFO [Ejb] stopping the embedded EJB container
      21:39:54,609 INFO [SessionFactoryImpl] closing