1 2 Previous Next 19 Replies Latest reply on May 3, 2013 4:52 PM by nickarls Go to original post
      • 15. Re: AS 7 Not inject EntityManager
        smarlow

        When you bootstrap the persistence unit through Persistence.createEntityManagerFactory, each persistence provider registered, will attempt to parse the persistence.xml and see if they are the right provider.  The first persistence provider that succeeds, returns the EntityManagerFactory as the winner.

         

        When the EE JPA container sees the persistence.xml in the deployment, the persistence.xml is parsed, a persistence unit is created (via PersistenceProvider.createContainerEntityManagerFactory() and @PersistenceUnit/PersistenceContexts in the deployment are injected.  The EE JPA container does a lot of magic as well (e.g. extended persistence context lifecycle, create/destroy transaction scoped persistence context, other stuff...).  Also, the contain magically sets integration properties so that the application doesn't have to (applications can override them).

         

        https://docs.jboss.org/author/display/AS72/JPA+Reference+Guide#JPAReferenceGuide-Hibernateproperties lists some of the Hibernate properties that the container automatically sets (if the application didn't already set them).

        • 16. Re: AS 7 Not inject EntityManager
          vitor.eduardods

          I have not made no changes, and suddenly, it works, using EntityManagerFactory, I mean. But, I need CDI.

          • 17. Re: AS 7 Not inject EntityManager
            vitor.eduardods

            Scott, I am continue in that torture. Using EntityManagerFactory works fine. However, cannot get an EntityManager via CDI. Please, please...

            • 18. Re: AS 7 Not inject EntityManager
              smarlow

              I'm not understanding what you mean exactly by "cannot get an EntityManager via CDI".  It might help others to help you, if you show the code that isn't working.  Also, try attaching the server.log (zipped up) with org.jboss.as.jpa trace logging enabled which should show what is happening.

              • 19. Re: AS 7 Not inject EntityManager
                nickarls

                EntityManagers are injected with the @PersistenceContext annotation per the EE spec. If you want to use CDI @Inject, you'll have to add a beans.xml to the deployment archive and then have a @Produces @EntityManager either as a producer field (which is @PC annotated so not much of a difference there) or from a producer method (where you have more freedom). CDI injected EntityManagers make more sense when you have e.g. Conversation scoped EM:s.

                 

                If you are saying that @PC doesn't work, you might want to display the complete deployment archive structure and where persistence.xml is in there.

                1 2 Previous Next