5 Replies Latest reply on Jan 26, 2006 11:30 PM by Bill Burke

    How to inject an EntityManager?

    Mike Osganian Newbie

      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