6 Replies Latest reply on Nov 9, 2005 5:56 AM by geraldloeffler

    ManagedPersistenceContext anyone?

    geraldloeffler

      hi,

      (referring to Seam 1.0beta)

      ManagedPersistenceContext doesn't work for me - it seem it's create() method is never executed. It also doesn't have a @Name...

      I'd like to use it in combination with SeamExtendedManagedPersistencePhaseListener, but there is no example for that exact set-up.

      thanks a lot,
      all the best,
      gerald

        • 1. Re: ManagedPersistenceContext anyone?
          gavin.king

          Follow the instructions here:

          http://docs.jboss.com/seam/reference/en/html/configuration.html#d0e1597

          ie.

          org.jboss.seam.init.managedPersistenceContexts bookingDatabase
          bookingDatabase.persistenceUnitName java:/EntityManagerFactories/bookingData

          and use

          org.jboss.seam.jsf.SeamExtendedManagedPersistencePhaseListener

          • 2. Re: ManagedPersistenceContext anyone?
            geraldloeffler

            thanks Gavin, but i had (of course ;-) followed the docs and when that didn't work i posted here

            To elaborate:

            If you look into org.jboss.seam.core.ManagedPersistenceContext.getEntityManagerFactory() you'll see that the persistenceUnitName must actually be without the "java:/EntityManagerFactories/"-prefix, so my seam.properties is

            org.jboss.seam.core.init.managedPersistenceContexts supplierdirDatabase
            


            persistence.xml matches this

            <entity-manager>
             <name>supplierdirDatabase</name>
            </entity-manager>
            


            And the session bean has the correct injection set-up:

            @In(create = true, required = true)
            EntityManager supplierdirDatabase;
            


            But this doesn't work:
            1. the EntityManager injected into my session bean is null (or it is not injected).
            2. there actually is a ManagedPersistenceContext instance:

            INFO [org.jboss.seam.Component] Component: supplierdirDatabase, scope: CONVERSATION, type: JAVA_BEAN, class: org.jboss.seam.core.ManagedPersistenceContext
            


            3. but there is no evidence (log-output, exception) that ManagedPersistenceContext.create() is ever executed, so there is probably no JNDI lookup for the EntityManagerFactory performed.
            4. ManagedPersistenceContext doesn't have a @Name (seems strange)

            thanks again,
            gerald


            • 3. Solution: ManagedPersistenceContext anyone?
              geraldloeffler

              In case anybody is interested in the solution to this:

              the scenario and code outlined above works just fine, it's just that the EntityManager is injected into the statefull session bean later than expected. In particular (and really not surprisingly), when the @PostConstruct callback of the session bean is executed, Seam will not have injected the EntityManager yet. The solution is thus to explicitly retrieve it from the Seam context in that callback (and rely on Seam injection in all business methods):

              @Stateful
              @Name("SupplierSearch")
              //...
              public class SupplierSearchController implements SupplierSearch, Serializable {
               @In(create = true, required = true)
               private EntityManager supplierdirDatabase;
              
               @PostConstruct
               public void init() {
               supplierdirDatabase = (EntityManager) Component.getInstance("supplierdirDatabase", true);
               // use supplierdirDatabase
               }
              
               public String search() {
               // use supplierdirDatabase, relying on its injection
               }
              }


              The Seam docs should still be corrected to reflect the correct name to be given to a managedPersistenceContext in seam.properties, though (see above).

              cheers,
              gerald


              • 4. Re: ManagedPersistenceContext anyone?
                rdewell

                Gerald, I think you need to use the Seam annotation @Create instead of @PostConstruct if you want to access Seam injected objects at that particular lifecycle. That may be off-topic to your original post, but it does keep you from having to do a lookup.

                • 5. Re: ManagedPersistenceContext anyone?
                  gavin.king

                   

                  "rdewell" wrote:
                  Gerald, I think you need to use the Seam annotation @Create instead of @PostConstruct if you want to access Seam injected objects at that particular lifecycle. That may be off-topic to your original post, but it does keep you from having to do a lookup.


                  Right.

                  • 6. Re: ManagedPersistenceContext anyone?
                    geraldloeffler

                    thank you both - @Create instead of @PostConstruct did indeed do the trick.

                    cheers,
                    gerald