JPA EntityManager尚未在struts2和JBOSS 7.1中初始化 [英] JPA EntityManager not initialized in struts2 and JBOSS 7.1

查看:89
本文介绍了JPA EntityManager尚未在struts2和JBOSS 7.1中初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Jboss提供的JPA库来启动基于struts2的应用程序. 我已经在standalone.xml中配置了数据源,我可以从jboss管理控制台中看到: 数据源已创建.并读取和处理presistence.xml文件. 但是,如果我在操作类中检查EntityManager实例.总是说空.

I am starting struts2 based application with Jboss provided JPA libraries. I have configured data-source in standalone.xml I can see from the jboss administration console that the datasource is created. and the presistence.xml files are read and processed. But if I check the EntityManager instance in Action Class. It always says null.

这是我的persistence.xml和Action类代码段

Here is my persistence.xml and Action class snippet

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
    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">

    <persistence-unit name="primary">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>java:jboss/datasources/mysqlDS</jta-data-source>

        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
            <!-- Properties for Hibernate -->
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
            <property name="hibernate.use_sql_comments" value="true" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
        </properties>

    </persistence-unit>

</persistence>

Struts2动作类:

Struts2 Action Class:

public class RegistrationAction extends  ActionSupport implements SessionAware,Preparable ,ModelDriven{

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

    @PersistenceContext(unitName="primary")
    private EntityManager em;

    @Override
    public Object getModel() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void prepare() throws Exception {
        if(em==null)
            System.out.println(" EM is null still..");
         //even Persistence.createEntityManagerFactory("primary"); returning NULL

    }

    @Override
    public void setSession(Map<String, Object> arg0) {
        // TODO Auto-generated method stub

    }

    public EntityManager getEm() {
        return em;
    }

    public void setEm(EntityManager em) {
        this.em = em;
    }

}

推荐答案

操作由 Struts容器管理.

EntityManager由 CDI容器管理.您可以将其注入CDI托管对象(EJB容器中的EJB,CDI托管Bean,Servlet,ecc ...),但不能将其注入Actions中.

EntityManager is managed by CDI Container. You can Inject it in CDI Managed objects (EJBs in EJB Container, CDI managed beans, servlets, ecc...), but not in Actions.

您需要使用 Struts2-CDI-Plugin 或执行查找以获取它.您甚至不应将其注入到Actions BTW中,最好使用业务组件(例如EJB)并对此进行查找.

You need to either use the Struts2-CDI-Plugin or to perform a lookup to get it. You should not even inject it in Actions BTW, it would be better to use a business component (for examle an EJB) and perform a lookup on that.

如果不使用CDI插件(例如,因为使用Spring插件),则

If not using the CDI Plugin (for example because using the Spring Plugin), the CDIUtil.java by Rob Veldpaus is perfect for this.

示例EJB:

@Stateless
public class MyEjb{

    @PersistenceContext(unitName="primary")
    EntityManager em;

    public Foo find(long id){
        return em.find(Foo.class, id);
    }
}

示例操作:

public class RegistrationAction extends ActionSupport 
                             implements SessionAware,Preparable ,ModelDriven {

    public String execute(){
        MyEjb ejb = new CdiUtil().lookup(MyEjb.class);
        System.out.println(ejb.find(1L));
        return SUCCESS;
    }

    /* your other stuff here */
}

这篇关于JPA EntityManager尚未在struts2和JBOSS 7.1中初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆