Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jpa Wildfly中的多个持久性单元?_Jpa_Wildfly_Persistence.xml - Fatal编程技术网

Jpa Wildfly中的多个持久性单元?

Jpa Wildfly中的多个持久性单元?,jpa,wildfly,persistence.xml,Jpa,Wildfly,Persistence.xml,Wildfly(9.0.2)应用程序中是否可能有两个持久性单元 我得到“WFLYJPA0061:Persistence unitName未指定,应用程序部署“jasper web.war”中有2个持久性单元定义。请将应用程序部署更改为只有一个持久性单元定义,或者为对持久性单元的每个引用指定unitName。” 我在@PeristenceContext注释中指定了unitName。我在某个我可以禁用的地方读过 <!-- <subsystem xmlns="urn:jboss:domai

Wildfly(9.0.2)应用程序中是否可能有两个持久性单元

我得到“WFLYJPA0061:Persistence unitName未指定,应用程序部署“jasper web.war”中有2个持久性单元定义。请将应用程序部署更改为只有一个持久性单元定义,或者为对持久性单元的每个引用指定unitName。”

我在
@PeristenceContext
注释中指定了
unitName
。我在某个我可以禁用的地方读过

<!--
<subsystem xmlns="urn:jboss:domain:jpa:1.1">
    <jpa default-datasource="" default-extended-persistence-inheritance="DEEP"/>
</subsystem>
-->

standalone.xml
中。这删除了错误消息,但也禁用了EntityManager的注入(在代码中引用EntityManager的空指针)

我曾尝试将持久性单元拆分为两个不同的ejb JAR,每个JAR都有自己的persistence.xml,但由于它们仍然包含在同一个war中,Wildfly仍然抱怨


这两个持久化单元用于hibernate,一个用于postgresql数据库,另一个用于ms access的ucanaccess驱动程序。它们都是单独工作的。

下面是一个在我们的wildfly 8.x/9.x ejb应用程序中工作的示例:

首先,在persistence.xml中定义每个持久性单元的所有类,可以关闭未列出的类以禁用自动发现

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<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">
      <jta-data-source>java:jboss/datasources/primary_ds</jta-data-source>
      <class>whatever.primary.model.SomeEntity</class>
      <exclude-unlisted-classes>true</exclude-unlisted-classes>
      <properties> ... </properties>
   </persistence-unit>

   <persistence-unit name="secondary">
      <jta-data-source>java:jboss/datasources/secondary_ds</jta-data-source>
      <class>whatever.secondary.model.AnotherEntity</class>
      <exclude-unlisted-classes>true</exclude-unlisted-classes>
      <properties> ... </properties>
   </persistence-unit>
</persistence>
Dao主要示例

package whatever.dao;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Stateless
public class DaoPrimaryExample {

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

    public void create(SomeEntity entity) {
        em.persist(entity);
    }
}
package whatever.dao;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Stateless
public class DaoSecondaryExample {

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

    public void create(AnotherEntity entity) {
        em.persist(entity);
    }
}
Dao次要示例

package whatever.dao;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Stateless
public class DaoPrimaryExample {

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

    public void create(SomeEntity entity) {
        em.persist(entity);
    }
}
package whatever.dao;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Stateless
public class DaoSecondaryExample {

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

    public void create(AnotherEntity entity) {
        em.persist(entity);
    }
}

重要提示:如果您计划在同一事务中使用booth持久化单元,则应使用XA数据源。

正如John Ament指出的,错误消息实际上指示一个没有单元名的注入点(我在源代码中忘记了…),我一摆脱它,一切都正常工作。我在谷歌上搜索这个问题时也有点困惑,当我在jboss中发现一些老问题时,这似乎曾经是一个问题

在persistence.xml中为persistence单元添加此选项修复了我的问题:

<property name="wildfly.jpa.default-unit" value="true"/>


是的,它确实有效。该错误表示存在未指定
unitName
的注入点。你确定你没有漏掉一个吗?如果你包括完整的错误,它可能会提供更多关于它所在位置的详细信息。谢谢,我有一个没有unitName的。现在部署没有错误。感谢您提供了非常全面的答案。这当然是可行的,是我有一个会话bean,其中有一个@PersistenceContext,而没有unitName,我忘记了。谢谢,我相信这也行得通。与此同时,我已经切换到sprng引导,并面临一系列全新的挑战:)