EntityManager返回始终为null

时间:2015-10-15 00:26:43

标签: java tomcat cdi

每当我请求entityManager它为null时,请参阅:

@Named
@ApplicationScoped
@AnBasicDAO
public class BasicDAOImpl implements BasicDAO, Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @PersistenceContext(unitName="generalPU")
    protected EntityManager entityManager;

这是我的persistence.xml:

<persistence-unit name="generalPU" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>     
        <non-jta-data-source>java:/comp/env/jdbc/testeDS</non-jta-data-source>
        <properties>
            <property name="hibernate.show_sql" value="false" />
            <property name="hibernate.format_sql" value="false" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
            <property name="hibernate.cache.use_second_level_cache"
                value="false" />
            <property name="hibernate.cache.use_query_cache" value="false" />
            <property name="hibernate.jdbc.batch_size" value="50" />            
        </properties>
    </persistence-unit>

这是我在server.xml(tomcat)中的资源:

<Context docBase="/home/usertest/Programas/apache-tomcat-7.0.59/webapps/cardoso" path="/cardoso" reloadable="false" source="org.eclipse.jst.jee.server:cardoso">
 <Resource name="jdbc/testeDS" auth="Container" type="javax.sql.DataSource"
          username="postgres"
          password="pgadmin"
          driverClassName="org.postgresql.Driver"
          url="jdbc:postgresql://localhost:5432/teste"
          maxTotal="25"
          maxIdle="10"
          validationQuery="select 1" />

</Context>

我的context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Context>
  <ResourceLink global="jdbc/sabrecadoDS" name="jdbc/testeDS" type="javax.sql.DataSource"/>
   <Manager pathname=""/> <!-- disables storage of sessions across restarts -->
   <Resource name="BeanManager"
      auth="Container"
      type="javax.enterprise.inject.spi.BeanManager"
      factory="org.jboss.weld.resources.ManagerObjectFactory"/>
</Context>

出了什么问题? EntityManager始终为null。前段时间我使用了Spring(现在是CDI),在applicationContext中我定义了并正常工作。

1 个答案:

答案 0 :(得分:1)

CDI不理解@PersistenceContext。所以改成它:

@Inject
protected EntityManager entityManager;

但是你必须在某处定义EntityManager,让我们说:

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Disposes;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.apache.deltaspike.jpa.api.transaction.TransactionScoped;

public class CdiApplicationConfig {

    @Inject
    private EntityManagerFactory emf;

    @Produces
    @ApplicationScoped
    public EntityManagerFactory createEntityManagerFactory() {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("generalPU");
        return emf;
    }

    public void close(@Disposes EntityManagerFactory emf) {
        emf.close();
    }

    @Produces
    @TransactionScoped // is a bit better than @RequestScoped because it won't allow perform injection outside transaction context
    public EntityManager createEntityManager() {
        return emf.createEntityManager();
    }

    public void close(@Disposes EntityManager em) {
        if (em.isOpen()) {
            em.close();
        }
    }
}

上面的代码使用DeltaSpike进行事务管理 - 但您可以使用任何您想要的内容。

还有一个提示:我不确定java:/comp中引用的数据源名称中persistence.xml之间是否允许斜杠,所以请检查:

  <non-jta-data-source>java:comp/env/jdbc/testeDS</non-jta-data-source>

如您所见,CDI与Spring非常相似。