1 Reply Latest reply on Jan 9, 2007 3:31 AM by f.ulbricht

    Sharing an EntityManager across two stateless session beans

    f.ulbricht Newbie

      There are two entities: Person and Address, the Person has a lazy set of Addresses.

      Furthermore there are two stateless session beans: the PersonManager can create and find Persons and the AddressManager can add new Addresses to the person. Both beans have an EntittyManager injected. The AddressManager has an injected PersonManager.

      This is the code of the AddressManager invoked by the client:

      @PersistenceContext
      private EntityManager em;
      @EJB
      private PersonManager personManager;
      ...
      public Address addAddress(Long personId, Address address) {
      Person person = this.personManager.findPerson(personId);
      this.em.persist(address);
      person.getAddresses().add(address);
      }

      This is the exception:
      org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.qualitype.entity.Person.addresses, no session or session was closed

      I would say both methods (addChild and findPerson) are running in the same transaction and sharing the same persistence context (even with two different EntityManagers). So the set should be loaded on demand. Why not?

      Has somebody an idea?