5 Replies Latest reply on Dec 3, 2007 6:02 PM by pmuir

    ManagedPersistenceContext in Manning JPA book

    asookazian

      The last chapter on Seam in the JPA with Hibernate book was very informative. I stumbled upon the section on triggering a LazyInitializationException when trying to root-cause analyze a colleague's LazyInitializationException problem.

      So it turns out that you can fix this problem by extending the persistence context as follows in your SFSB:

      @PersistenceContext(type=EXTENDED)
      private EntityManager em;


      When I search for keyword "ManagedPersistenceContext" in all of the Seam example apps in the CR2 distro, I got no hits. Does that mean it's not necessary to use the Seam managed context and the TransactionalSeamPhaseListener to solve LazyInitializationException's?

      When do you need to integrate the Seam-managed persistence context as explained in pages 814-815 of the book? I guess I'm not understanding exactly the added value of using the Seam-managed persistence context versus javax.persistence.PersistenceContext.

      I see more methods in the API for the Seam-managed persistence context class...

        • 1. Re: ManagedPersistenceContext in Manning JPA book
          amitev

          It's not necessary, but it's strongly recommended to use seam managed persistence context. With SMPC you can use features like s:convertEntity, EntityQuery, EntityHome ...etc.

          • 2. Re: ManagedPersistenceContext in Manning JPA book
            delphi'sghost

            My understanding of it is that :

            @PersistenceContext (EJB PC) - Single shot persistence context for the duration of the method call

            @PersistenceContext(Type=EXTENDED) (Extended EJB PC) - Persistence context that lasts for the duration of the session bean, but not shareable across session beans.

            @In private EntityManager entityManager (SMPC) - Seam Managed Persistence Context that is tied to the conversation and can be used in multiple method calls, and across multiple beans. As amitev mentions, you get a lot of extras for free with the SMPC.

            The SMPC also gets rid of lazy initialization errors by using two transactions on a request. One to commit the work, and then another to let you read database entities when rendering the view if you use the Seam transactional phase listener.

            If you are working in a Seam-Gen generated app, you should be ready to start using SMPCs already without requiring any tweaking with the config.

            You use SMPCs in your beans by injecting the entityManager using the following :


            @In
            private EntityManager entityManager;
            


            • 3. Re: ManagedPersistenceContext in Manning JPA book
              amitev

              Very detailed post. Thank you!

              • 4. Re: ManagedPersistenceContext in Manning JPA book
                segev

                We decided to use SMPC for all the reasons highlighted in the previous post but there is a catch if you use it in a Session scoped stateful session bean. the injected entity manager will have a scope of Conversation (by default) and transactions will not be committed properly. This can be demonstrated by changing the Seam supplied messages example to use SMCP instead of the original EJB extended PC.
                Is it OK to change the default scope of the SMPC component to session scope?
                Can the above SMPC be used for conversations?

                • 5. Re: ManagedPersistenceContext in Manning JPA book
                  pmuir

                  No, you shouldn't change the scope of an SMPC. Use an extended PC or http://wiki.jboss.org/wiki/Wiki.jsp?page=SeamEntityHomeForLongRunningContexts