1 Reply Latest reply on Feb 14, 2011 3:26 AM by jaikiran

    entityManager is null -- not being injected into my jpa -- please help

    davestar

      I have hibernate entities integrated with spring.  I have a GenericDaoJpa where I inject the EntityManager.  When I access entityManager for any queries the entityManager is null.  I do not see any errors or issues on JBoss startup where all of this is deployed.
      When I call the genericDao.getAll() method and on inspection shows that entityManager is null and has not been injected.

      Can you please help or point me in the direction to investigage this further?

       

      My persistence.xml

       

       

      <persistence-unit name="ties-2">

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

            <jta-data-source>java:/ties-2Datasource</jta-data-source>

            <!-- The <jar-file> element is necessary if you put the persistence.xml in the WAR and the classes in the JAR -->

            <properties>

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

               <property name="hibernate.show_sql" value="true"/>
               <property name="hibernate.format_sql" value="true"/>
               <property name="jboss.entity.manager.factory.jndi.name" value="java:/ties-2EntityManagerFactory"/>
            </properties>
      </persistence-unit>

       

       

      My applicationContext.xml entry

       

      <jee:jndi-lookup id="entityManagerFactory" jndi-name="java:/ties-2EntityManagerFactory"/>

       

       

      My java: Namespace in JBoss JMX console

       

      java: Namespace
        +- ties-2EntityManagerFactory (class: org.jboss.jpa.injection.InjectedEntityManagerFactory)

       

      Global jndi namespace in JBoss JMX console

       


      +- persistence.unit:unitName=ties2.war (class: org.jnp.interfaces.NamingContext)
        |   +- #ties-2 (class: org.hibernate.impl.SessionFactoryImpl)

       

      My GenericDaoJpa.java

       

      package com.rtd.ties2.dao;

      import java.math.BigDecimal;
      import java.util.List;
      import javax.persistence.EntityManager;
      import javax.persistence.PersistenceContext;
      import org.apache.commons.logging.Log;
      import org.apache.commons.logging.LogFactory;
      import com.rtddenver.gwt.client.dto.entity.DomainObject;

      public class GenericDaoJpa<T extends DomainObject> implements GenericDao<T> {

       


      private Class<T> type;
      private static final Log log = LogFactory.getLog("GenericDaoJpa");
      protected EntityManager entityManager;

       

      @PersistenceContext(unitName="ties-2")
      public void setEntityManager(EntityManager entityManager){
        this.entityManager = entityManager;
      }

       

      public GenericDaoJpa(Class<T> type){
        super();
        this.type = type;
      }

      @Override
      //@Transactional(readOnly=true)
      public List<T> getAll() {
        return entityManager.createQuery("select o from " +
          type.getName() + " o").getResultList();   
      }

      @Override
      public void save(T object) {
        entityManager.persist(object); 
      }

      @Override
      public void delete(T object) {
        entityManager.remove(object);
      }

      @Override
      public T merge(T object) {
        log.debug("merging instance");
        try {
         return entityManager.merge(object);
        } catch (RuntimeException re) {
         log.error("merge failed", re);
         throw re;
        }
      }

      }