Glassfish嵌入时@PersistenceContext为null(无ejb)

时间:2014-06-08 18:34:29

标签: java hibernate java-ee glassfish jta

我面临以下问题。在我的应用程序中,我尝试使用Glassfish嵌入式服务器将@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.xmlresources/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>

现在我想知道我的配置有什么问题?有什么建议?

感谢。

0 个答案:

没有答案