【发布时间】:2015-10-15 00:26:42
【问题描述】:
每次我请求 entityManager 时它都为空,请参阅:
@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 始终为空。前段时间我使用了 Spring(现在是 CDI)并在 applicationContext 中定义了并正常工作。
【问题讨论】:
-
您如何获得对您的
BasicDAOImpl的引用? -
对不起,我没有理解你的问题。
-
CDI 在 tomcat 中无法开箱即用。您需要一个 JavaEE 应用程序服务器,例如 TomEE 或 Glassfish。使用 tomcat,需要使用独立的 CDI 容器,例如 Weld。
-
我使用 Weld 作为 CDI 实现