5 Replies Latest reply on Jan 26, 2006 11:30 PM by bill.burke

    How to inject an EntityManager?

    osganian

      Hi I'm using JBoss 4.0.3SP1 (installed using installer and selecting the EJB3 configuration) and I am unable to figure out how to get an EJB3 EntityManager injected into my code.

      persistence.xml:

      <entity-manager>
       <name>myEM</name>
       <provider>org.hibernate.ejb.HibernatePersistence</provider>
       <jta-data-source>java:/Test</jta-data-source>
       <properties>
       <property name="hibernate.dialect"
       value="org.hibernate.dialect.MySQLDialect"/>
       <property name="hibernate.connection.driver_class"
       value="com.mysql.jdbc.Driver"/>
       <property name="hibernate.connection.username"
       value="tester"/>
       <property name="hibernate.connection.password"
       value="tester"/>
       <property name="hibernate.connection.url"
       value="jdbc:mysql://localhost:3306/"/>
       <property name="hibernate.max_fetch_depth" value="3"/>
       <property name="hibernate.show_sql" value="true"/>
       </properties>
      </entity-manager>
      


      Test class that needs the injection:
      public class TestClass {
       @PersistenceContext(unitName="myEM")
       protected EntityManager entityManager;
       ...
      }
      


      When the code in TestClass gets run the entityManager is NULL.

      My EAR has a PAR that contains my persistence.xml file. And the TestClass is inside my WAR which is also bundled in the EAR.

      Any ideas? I'm not getting any errors in the jboss log. Is there any file changes I have to make to the default installation?

      Thanks for any help!
      Mike


        • 1. Re: How to inject an EntityManager?
          bill.burke

          you can't inject arbitrarily into any class. Injection works only into EJBs, interceptors, and when we support it, servlets/JSPs. Relaly only enterprise components.

          • 2. Re: How to inject an EntityManager?
            osganian

            Thanks for the reply. So what are my choices? Do I have to create an InitialContext and look up the EntityManager via JNDI?

            I tried and I get an InjectedEntityManager back which throws an exception if I try and get the Transaction from it. If I don't begin a transaction I get an TransactionRequiredException. So what do I do? Is it possible to get an ExtendedEntityManager versus an InjectedEntityManager?

            Thanks for any help!
            Mike

            • 3. Re: How to inject an EntityManager?
              bill.burke

              you want an extended entity manager? Lookup the factory in jndi after you've declared jboss.entity.manager.factory.jndi.name and create it from the factory.

              • 4. Re: How to inject an EntityManager?
                osganian

                I don't know, do I want an ExtendedEntityManager to do this?

                Basically, what is the perferred way to obtaining an EntityManager in a POJO running within JBoss?

                Doing:

                InitialContext ctx = new InitialContext();
                EntityManager em = (EntityManager) ctx.lookup("java:/EntityManagers/TestEM");
                


                Returns an EntityManager of type InjectedEntityManager. Which I guess is fine but I get an IllegalStateException if I call:

                em.getTransaction(); (to begin() & commit()/rollback())
                


                And if I don't call em.getTransaction() and just call em.find(...) I get a TransactionRequiredException.

                Damn if I do damn if I don't!

                Thanks again for any suggestions!
                Mike


                • 5. Re: How to inject an EntityManager?
                  bill.burke

                  sorry, i gave you directions on how to obtain factory/entitymanager for the next release which will hopefully be tomorrw.