EntityManager未注入MDB(尽管其他资源是)

时间:2014-05-29 16:44:39

标签: java-ee entitymanager

在以下代码中,队列是正确注入的,但实体管理器不是。我对JavaEE不是很有经验,但我不知道它为什么不起作用。任何提示都非常感谢!

我使用OpenEJB作为应用程序容器。使用以下代码,正确注入QueueConnectionFactory;但是,EntityManager始终是null。据我所知,可以在托管实体(即Bean)中使用@PersistenceContext来获取它。或者我是否需要在ejb-jar.xml文件中定义它?

以下是我的代码:

MDB:

 public class ServerBean implements MessageListener {

 @Resource
 private ConnectionFactory factory;

 @Resource(name = "queue")
 private Queue queue;

 @PersistenceContext
 private EntityManager em;

  //methods...
 }

ejb-jar.xml中:

<ejb-jar xmlns="http://java.sun.com/xml/ns/javaee" metadata-complete="true">
<enterprise-beans>
    <message-driven>
        <ejb-name>ServerBean</ejb-name>
        <ejb-class>foo.ServerBean</ejb-class>

        <messaging-type>javax.jms.MessageListener</messaging-type>

        <activation-config>
            <activation-config-property>
                <activation-config-property-name>destination</activation-config-property-name>
                <activation-config-property-value>ServerBean</activation-config-property-value>
            </activation-config-property>
            <activation-config-property>
                <activation-config-property-name>destinationType</activation-config-property-name>
                <activation-config-property-value>javax.jms.Queue</activation-config-property-value>
            </activation-config-property>
        </activation-config>

        <resource-ref>
            <res-ref-name>java:comp/env/foo.ServerBean/factory</res-ref-name>
            <res-type>javax.jms.ConnectionFactory</res-type>
            <injection-target>
                <injection-target-class>foo.ServerBean</injection-target-class>
                <injection-target-name>factory</injection-target-name>
            </injection-target>
        </resource-ref>

        <resource-env-ref>
            <resource-env-ref-name>java:comp/env/queue</resource-env-ref-name>
            <resource-env-ref-type>javax.jms.Queue</resource-env-ref-type>
            <mapped-name>queue</mapped-name>
            <injection-target>
                <injection-target-class>foo.ServerBean</injection-target-class>
                <injection-target-name>queue</injection-target-name>
            </injection-target>
        </resource-env-ref>

    </message-driven>

</enterprise-beans>
</ejb-jar>

persitence.xml:

<?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="fooUnit" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>foo_ds</jta-data-source>
    <class>foo.model.Bar</class>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>

    <properties>
    <property name="hibernate.connection.driver_class" value="org.h2.Driver" />
        <property name="hibernate.connection.url" value="jdbc:h2:/tmp/database/dst;AUTO_SERVER=TRUE;MVCC=true" />
        <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
        <property name="hibernate.hbm2ddl.auto" value="create-drop" />
    <property name="javax.persistence.validation.mode" value="NONE"/>
    <property name="hibernate.query.factory_class" value="org.hibernate.hql.classic.ClassicQueryTranslatorFactory" />
    </properties>
</persistence-unit>
</persistence>

0 个答案:

没有答案
相关问题