4 Replies Latest reply on Nov 26, 2013 3:03 AM by Vahid Vafaei

    Question regarding application of @PersistenceContext (CMP)

    Vahid Vafaei Newbie

      Hello,

       

      Before I move forward; I am using JBoss EAP 6.1

      Currently I am going to use Container Managed Persistence in my code. After reading a lot of tutorials (everywhere) and posts (here) I am still facing difficulties on getting it all to work.

      Eventually I keep getting a "null" when I want to see if there is actually an EntityManager.

       

      So this is what I have:

       

      persistence.xml

      .......

      <persistence-unit name="TestEntityManager" transaction-type="JTA">

           <provider>org.hibernate.ejb.HibernatePersistence</provider>

           <jta-data-source>java:/MySqlDS</jta-data-source>

           <class>com.test.content.engine.db.entities.TestEntity</class>

           <properties>

               <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>

               <property name="hibernate.hbm2ddl.auto" value="update"/>

          </properties>

      </persistence-unit>

      .......

       

      DoingTestEntityManager class (very simple and straight forward)


      @Stateful

      public class DoingTestEntityManager implements Serializable {

           private static final long serialVersionUID = 1L;

       

           @PersistenceContext(unitName="TestEntityManager", type = PersistenceContextType.EXTENDED)

           protected EntityManager entityManager;

       

           public DoingTestEntityManager() {

             }

       

           public TestEntity getTestEntityByID(long id) {

                TestEntity testEntity = entityManager.find(TestEntity.class, id);

                return testEntity;

           }

      }

       

       

      Somehow it seems that the DoingTestEntityManager is not being registered within JBoss, while I do have another bean (although this is a MessageDrivenBean) which does get registered.

      This also results in "EntityManager" being null.

       

      I have a class which does the calls on "DoingTestEntityManager" class through InitialContext lookup or by using the @EJB annotation, but is not able to find anything. I assume that just initiating the class (class x = new class) is not applicable for an Stateful bean.

       

      There must be something here that I am missing, if anyone is able to provide some guidance or help in the right direction, that would be much appreciated.

      Thanks in advance.