Wildfly中有多个持久化单元?

7
在Wildfly(9.0.2)应用程序中是否可能有两个持久化单元?
我收到错误信息“WFLYJPA0061:未指定Persistence unitName并且在应用程序部署depolyment“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文件中,我删除了相关内容的错误提示信息,但也禁用了实体管理器的注入(代码中引用它们时会出现空指针问题)。

我尝试将持久化单元分成两个不同的ejb-jar文件,每个文件都有自己的persistence.xml,但由于它们仍然包含在同一个war文件中,Wildfly仍然会报错。

这两个持久化单元与Hibernate一起使用,一个使用PostgreSQL数据库,另一个使用UCanAccess驱动程序连接MS Access。它们都可以单独正常工作。


是的,它可以工作。错误表明存在一个注入点没有指定unitName。你确定没有遗漏吗?如果包括完整的错误信息,可能会提供更多关于其位置的详细信息。 - John Ament
谢谢,我之前有一个没有 unitName 的版本。现在已经可以正常部署了。 - jon martin solaas
我已经定义了持久化单元的名称,但仍然遇到了错误。请确保设置unitName而不是name - Queeg
3个回答

12

这里是我们在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>
如果您使用JBoss Developer Studio,请忽略警告(这只是Eclipse的一个缺陷):
多个持久性单元被定义 - 只有第一个持久性单元将被识别。 Resources.java
package whatever.util;

import javax.annotation.Resource;
import javax.enterprise.inject.Produces;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.sql.DataSource;

public class Resources {

    @Produces
    @PersistenceContext(unitName = "primary")
    private EntityManager emPrimary;

    @Produces
    @PersistenceContext(unitName = "secondary")
    private EntityManager emSecondary;

    @Produces
    @Resource(lookup = "java:jboss/datasources/primary_ds")
    private DataSource dsPrimary;

    @Produces
    @Resource(lookup = "java:jboss/datasources/secondary_ds")
    private DataSource dsSecodnary;

}

道的主要例子

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);
    }
}

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


非常感谢您提供如此详尽的答案。它确实有效,是我自己忘记了一个带有@PersistenceContext但没有unitName的会话bean。 - jon martin solaas

12

在persistence.xml文件中为持久化单元添加此选项,解决了我的问题:

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

谢谢,我相信那也可以。与此同时,我已经转向了Spring Boot,并面临着全新的一系列挑战 :) - jon martin solaas

5

正如John Ament指出的那样,错误消息实际上表明存在一个没有unitName的注入点(我在源代码中忘记了这个)。一旦我解决了这个问题,一切都按照应有的方式工作了。当我在谷歌上搜索此问题时,我也有些困惑,因为实际上在jboss AS中,这似乎曾经是一个问题。


虽然错误信息表明存在这样的问题,但它并没有指出导致问题的注入点。你仍然需要自己搜索。 - Queeg

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接