@PersistenceContext
注入到DAO的NON ejb实例中。所以它是由CDI控制的pojo。像这样:
@Named
public class DummyDAO implements Serializable {
@PersistenceContext private EntityManager entityManager;
public void testManager() {
if (entityManager == null) {
throw new RuntimeException("It's null!!");
}
System.out.println("Hello! I'm injected");
}
}
这个虚拟DAO通过CDI注入JSF bean,如下所示:
@ManagedBean
@SessionScoped
public class OrderImportManagerBean implements Serializable {
@Inject
DummyDAO dummyDAO;
public void testManager() {
dummyDAO.testManager();
}
}
testManager()
来自xhtml页面,如#{orderImportManagerBean.testManager}
entityManager
始终为null
。 CDI本身可以工作,所有实例都应该注入,但不能注入这个实例。我在persistence.xml
目录下有orm.xml
和resources/META-INF
。因此,在打包项目之后,我得到META-INF
并将内容放在正确的位置(classes/META-INF
)。这是我的persistence.xml
:
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="persistance-unit" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:mem:testdb"/>
<property name="javax.persistence.jdbc.user" value="sa"/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="hibernate.dialect"
value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name = "hibernate.show_sql" value = "true" />
</properties>
</persistence-unit>
</persistence>
现在我想知道我的配置有什么问题?有什么建议?
感谢。