0 Replies Latest reply on Sep 22, 2006 10:30 AM by Davide Bettoni

    Why this:

    Davide Bettoni Newbie

      look at this code:

      @Stateless
       @Remote
       public class SMSBAccountManager implements AccountManager {
      
       @PersistenceContext //(unitName="smsboss_core")
       protected EntityManager em;
      
       ...
       ...
      
       public Set<Account> getAccounts( int providerId ) {
       Provider p = em.find( Provider.class, providerId );
       Set<Account> set = p.getAccounts();
       for( Account a : set ){
       System.out.println("@@@@ s="+a);
       }
       return p.getAccounts();
       }
       }
      
      

      when I run my remote junit test and I scan the Set returned by the method getAccounts(...)
      I haven't any problems
      but if I drop the useless code:

      public Set<Account> getAccounts( int providerId ) {
       Provider p = em.find( Provider.class, providerId );
       //Set<Account> set = p.getAccounts();
       //for( Account a : set ){
       // System.out.println("@@@@ s="+a);
       //}
       return p.getAccounts();
       }
      

      my test fail with exception:

      [junit] Testsuite: it.menhir.j2ee.smsboss.ejb3.core.TestSuite
      [junit] Tests run: 3, Failures: 0, Errors: 1, Time elapsed: 3,054 sec
      [junit] ------------- Standard Error -----------------
      [junit] 22-set-2006 15.56.46 org.hibernate.LazyInitializationException
      [junit] GRAVE: failed to lazily initialize a collection of role: it.menhir.j2ee.smsboss.ejb3.core.entity.Provider.accounts, no session or session was closed
      [junit] org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: it.menhir.j2ee.smsboss.ejb3.core.entity.Provider.accounts, no session or session was closed
      [junit] at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)
      [junit] at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
      [junit] at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:343)
      [junit] at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
      [junit] at org.hibernate.collection.PersistentSet.iterator(PersistentSet.java:138)
      [junit] at it.menhir.j2ee.smsboss.ejb3.core.AccountWithMessageTest.addMessages(AccountWithMessageTest.java:55)
      [junit] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      [junit] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      [junit] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      [junit] at java.lang.reflect.Method.invoke(Method.java:585)
      [junit] at org.junit.internal.runners.TestMethodRunner.executeMethodBody(TestMethodRunner.java:99)
      [junit] at org.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRunner.java:81)
      [junit] at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
      [junit] at org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75)
      [junit] at org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45)
      [junit] at org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMethodsRunner.java:71)
      [junit] at org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:35)
      [junit] at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42)
      [junit] at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
      [junit] at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
      [junit] at org.junit.internal.runners.CompositeRunner.run(CompositeRunner.java:29)
      [junit] at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42)
      [junit] at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
      [junit] at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
      [junit] at junit.framework.JUnit4TestAdapter.run(JUnit4TestAdapter.java:32)
      [junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:297)
      [junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.launch(JUnitTestRunner.java:672)
      [junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(JUnitTestRunner.java:567)
      [junit] ------------- ---------------- ---------------
      [junit] Testcase: addMessages(it.menhir.j2ee.smsboss.ejb3.core.AccountWithMessageTest): Caused an ERROR
      [junit] failed to lazily initialize a collection of role: it.menhir.j2ee.smsboss.ejb3.core.entity.Provider.accounts, no session or session was closed
      [junit] org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: it.menhir.j2ee.smsboss.ejb3.core.entity.Provider.accounts, no session or session was closed
      [junit] at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)
      [junit] at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
      [junit] at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:343)
      [junit] at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
      [junit] at org.hibernate.collection.PersistentSet.iterator(PersistentSet.java:138)
      [junit] at it.menhir.j2ee.smsboss.ejb3.core.AccountWithMessageTest.addMessages(AccountWithMessageTest.java:55)
      [junit] at org.junit.internal.runners.TestMethodRunner.executeMethodBody(TestMethodRunner.java:99)
      [junit] at org.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRunner.java:81)
      [junit] at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
      [junit] at org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75)
      [junit] at org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45)
      [junit] at org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMethodsRunner.java:71)
      [junit] at org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:35)
      [junit] at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42)
      [junit] at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
      [junit] at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
      [junit] at org.junit.internal.runners.CompositeRunner.run(CompositeRunner.java:29)
      [junit] at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42)
      [junit] at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
      [junit] at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
      [junit] at junit.framework.JUnit4TestAdapter.run(JUnit4TestAdapter.java:32)


      does someone know to make sense to this behavior?