Hibernate java.lang.IllegalArgumentException:WFLYWELD0037:将持久性单元注入CDI托管bean时出错

Hibernate java.lang.IllegalArgumentException:WFLYWELD0037:将持久性单元注入CDI托管bean时出错,hibernate,jpa,wildfly,jboss7.x,java-ee-8,Hibernate,Jpa,Wildfly,Jboss7.x,Java Ee 8,我看到的错误与此类似。然而,在我的EAR和WAR项目之间并没有直接的ejb注入 环境: 应用服务器:JBoss EAP 7.3 我将项目设置为: EJB-EAR: 包含一个DataManagement.jar,其中包含所有DAO对象、JPA相关内容和persistence.xml persistence.xml位于jar中的main/resources/META-INF/下 persistence.xml: <persistence version="2.1"

我看到的错误与此类似。然而,在我的EAR和WAR项目之间并没有直接的ejb注入

环境: 应用服务器:JBoss EAP 7.3

我将项目设置为: EJB-EAR:

  • 包含一个DataManagement.jar,其中包含所有DAO对象、JPA相关内容和persistence.xml
  • persistence.xml位于jar中的main/resources/META-INF/下
persistence.xml:

<persistence version="2.1"
   xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
        http://xmlns.jcp.org/xml/ns/persistence
        http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
   <persistence-unit name="primary">
      <!-- If you are running in a production environment, add a managed
         data source, this example data source is just for development and testing! -->
      <jta-data-source>java:jboss/datasources/PcosDS</jta-data-source>
      <properties>
         <!-- Properties for Hibernate -->
         <property name="hibernate.hbm2ddl.auto" value="create-drop" />
         <property name="hibernate.show_sql" value="false" />
      </properties>
   </persistence-unit>
</persistence>
生产者的定义是:

public class Resources {
      @Produces
      @PersistenceContext(unitName="primary")
      private EntityManager em;
}
战争:

  • WAR项目启动应用程序,并在其WEB-INF/lib中包含其他JAR。其中一个jar最终通过EJB查找访问jar-in-EAR项目
  • WAR在/src/main/webapp/META-INF下包含jboss-deployment-structure.xml和jboss-all.xml
jboss-deployment-structure.xml

<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.2">
    <deployment>
        <dependencies>            
            <module name="deployment.pts-ear-1.0-SNAPSHOT.ear.DataManagement.jar"/>
        </dependencies>
        <exclusions>
        
        </exclusions>
    </deployment>
</jboss-deployment-structure>

jboss-all.xml

<jboss umlns="urn:jboss:1.0">
    <jboss-deployment-dependencies xmlns="urn:jboss:deployment-dependencies:1.0">
        <dependency name="pts-ear-1.0-SNAPSHOT.ear" />
    </jboss-deployment-dependencies>
</jboss>

EAR部署得很好,我确实看到测试数据库预填充了初始数据。当我部署war项目时,问题就出现了,我看到的错误是

原因:java.lang.IllegalArgumentException:WFLYWELD0037:错误 将持久性单元注入CDI管理的bean。找不到 部署中名为“主”的持久化单元 初始设置Servlet-war-1.0-SNAPSHOT.war用于注入点专用 javax.persistence.EntityManager com.lmco.pts.pcos.inf.DataManagement.dao.Resources.em 在org.jboss.as.weld上。jpa@7.3.0.GA-redhat-00004//org.jboss.as.weld.services.bootstrap.WeldJpaInjectionServices.GetScopedPaname(WeldJpaInjectionServices.java:105) 在org.jboss.as.weld上。jpa@7.3.0.GA-redhat-00004//org.jboss.as.weld.services.bootstrap.WeldJpaInjectionServices.registerPersistenceContextInjectionPoint(WeldJpaInjectionServices.java:68) 在org.jboss.weld上。core@3.1.2.Final-redhat-00001//org.jboss.weld.injection.ResourceInjectionFactory$PersistenceContextResourceInjectionProcessor.getResourceReferenceFactory(ResourceInjectionFactory.java:174) 在org.jboss.weld上。core@3.1.2.Final-redhat-00001//org.jboss.weld.injection.ResourceInjectionFactory$PersistenceContextResourceInjectionProcessor.getResourceReferenceFactory(ResourceInjectionFactory.java:162)


如果我将DataManagement.jar打包到WAR项目的WEB-INF/lib中,那么一切都可以正常工作。我正在尝试将DataManagement.jar移出war项目,以便其他项目可以使用它,并作为部署在应用服务器上的其他项目的动态资源。我怀疑持久性单位在某种程度上是不可见的战争,但战争并不真正需要持久性单位。它只是从执行数据库访问的JAR调用服务。

如果我理解正确,您假设一个单独的JAR是一个专用的部署,您可以直接引用,但事实并非如此。持久化单元必须是部署的一部分。也许可以为JAR创建一个JBoss模块并引用它,但最终,它将为引用该模块的每个部署启动持久化单元。把这个JAR放进WEB-INF/lib有什么不对?您也可以将JAR打包到其他WAR中。

结果发现,我没有发布的缺失信息之一是DataManagement.JAR的beans.xml配置。其bean的发现模式设置为all,以允许发现未注释的生产者类资源:

<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
      http://xmlns.jcp.org/xml/ns/javaee
      http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
    bean-discovery-mode="all">
</beans>

部署的WAR能够通过执行ejb查找成功访问DataManagement.jar。

DataManagement.jar和WAR是否在同一个EAR中?EAR中只打包了DataManagement.jar。其思想是允许多个WAR访问同一个jar。实际上,我设法解决了这个问题,它与beans.xml和producer类的组合有关。我将很快发布解决方案。
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
      http://xmlns.jcp.org/xml/ns/javaee
      http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
    bean-discovery-mode="all">
</beans>
@Stateless
public class Resources {
      @PersistenceContext(unitName="primary")
      private EntityManager em;
      
      @Produces
      public EntityManager entityManager(){
          return em;
      }
}