EntityManager未使用@PersistenceContext注入无状态会话Bean

时间:2017-09-29 10:32:13

标签: java hibernate jpa ejb

我一直在查看很多类似的问题,这些问题并没有反映出我的确切问题。如果我忽略了某人已经解决了这个问题,请告诉我。

我目前正在Wildfly 10.1上将旧的EJB CMP bean迁移到JPA。 这里的问题是我的无状态会话bean中的实体管理器没有注入@PersistenceContext,使EntityManager保持为NULL。我尝试使用EntityManagerFactory解决这个问题。

附上来自服务器端的persistence.xml,EJB2会话Bean代码。

请帮忙。

public class CAAFAdminServiceBean implements SessionBean {
        @PersistenceContext(unitName = "PFJPAEMJTA")
            private EntityManager em1;
            // For some reason, em1 is not initialized by EJB container though this is Session bean, ideally it should be.
            // Hence calling getEntityManager() to create EntityManager from its Factory.
            private EntityManager em = null;

        public void ejbCreate() {
        em = getEntityManager();
        // Set same object in the Util class so that same can be referenced anywhere.
                CAAFEntityUtil.setEntityManager(em);
        }

        private EntityManager getEntityManager() {
                if (em1 == null) {
                    if (em == null) {
                        PerfLog perflog = new PerfLog(new PerfLogEJBCalledCategory(this.getClass(), "getEntityManager"));
                        String status = PerfLog.FAIL;
                        try {
                            EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("PFJPAEMJTA");
                            em1 = em = entityManagerFactory.createEntityManager();
                            status = PerfLog.SUCCESS;
                            return em;
                        } finally {
                            perflog.end(status);
                        }
                    }
                    return em;
                }
                System.out.println("************EntityManager object is injected by EJB container, great to see that!!!************");
                return em1;
            }

        }


<?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="PFJPAEMJTA" transaction-type="JTA">
            <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
            <jta-data-source>java:jboss/jdbc/PF_TX_DATASOURCE</jta-data-source>
            <class>com.entity.rdbms.RDBMSComponentBean</class>   
            <properties>
                <property name="showSql" value="true"/>
                <property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect" />
                <property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform" />
            </properties>
        </persistence-unit>
    </persistence>

1 个答案:

答案 0 :(得分:0)

EJB 2会话bean不支持注入。考虑将其转换为&#34;现代&#34; EJB(假设它在现有的ejb-jar.xml中声明为Stateless):

@Stateless
public class CAAFAdminServiceBean {

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

    @PostConstruct
    public void formerEjbCreate() {
        CAAFEntityUtil.setEntityManager(em);
    }

    public void someBusinessMethod(MyEntity myEntity) {
        em.persist(myEntity);
        ...
    }

    ...
}

并删除ejb-jar.xml文件中对它的任何引用。

考虑删除CAAFEntityUtil,因为您应该能够在任何地方注入EntityManager,因为您也有CDI支持。