0 Replies Latest reply on Jan 31, 2007 9:01 PM by jtalafous

    org.hibernate.LazyInitializationException: failed to lazily

      I'm on the learning curve when it comes to using EJB 3.0 inside a
      JBoss EJB container. I have developed all my entity beans and can
      persist them successfully with JPA annotations outside an EJB
      container (using Hibernate only) without a problem. it is only when I
      am migrating to JBoss that I am having problems:

      1.) I have an Entity which has several Lists as fields that get persisted.

      2.) I have a stateful session bean (SFSB) which has the entityManager
      annotated with @PersistenceContext(unitName = "myPU",
      type=PersistenceContextType.EXTENDED)
      and this works great to persist an entity and immediately retrieve it
      in the same method. Without this annotation, retrieving the Entity
      will throw this exception:

      org.hibernate.LazyInitializationException: failed to lazily initialize
      a collection of role: MyEntityClass, no session or session was closed

      3.) I can persist the Entity and immediately retrieve it in the same
      method. However, if I keep JBoss running, and restart the VM and
      retrieve the Entity with the exact same SFSB again, the exception in
      bullet 2 above is thrown. (BTW, Setting to EAGER loading isn't an
      option for my requirements.)

      It seems that the entityManager session is being closed in one way or
      another, despite the inclusion of the annotation in bullet 2 above.

      I'm not clear as to if this should happen or not. I am assuming that
      a stateful session bean should be able to hold open a JPA
      entityManager session indefinitely. Is this a good assumption?

      Also, looking at the JBoss server.log, I am seeing that my SFSB is
      being passivated. How is the entityManager session supposed to
      progress through passivation? I would expect it wouldn't. After
      activation, am I supposed to somehow restore the entityManager
      session?

      Thanks in advance for any tip or hints to get me going.