Wildfly 中的多个持久性单元?
Multiple persistence units in Wildfly?
是否可以在 Wildfly (9.0.2) 应用程序中有两个持久性单元?
我得到 "WFLYJPA0061: Persistence unitName was not specified and there are 2 persistence unit definitions in application deployment deployment "jasper-web.war”。要么将应用程序部署更改为只有一个持久性单元定义,要么为每个对持久性单元的引用指定 unitName。”
我在 @PeristenceContext
注释中指定了 unitName
。我在某个可以禁用的地方阅读
<!--
<subsystem xmlns="urn:jboss:domain:jpa:1.1">
<jpa default-datasource="" default-extended-persistence-inheritance="DEEP"/>
</subsystem>
-->
在 standalone.xml
中。这删除了错误消息,但也禁用了 entityManagers 的注入(在代码中引用它们的空指针)
我试图将持久性单元拆分到两个不同的 ejb-jars,每个都有自己的 persistence.xml,但由于它们仍然包含在同一个 war 中,Wildfly 仍然抱怨。
这两个持久化单元与hibernate 一起使用,一个与postgresql 数据库一起使用,另一个与用于ms 访问的ucanaccess 驱动程序一起使用。他们都分开工作。
这里是一个在我们的 wildfly 8.x/9.x ejb app 中工作的例子:
首先为 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 缺陷):
Multiple persistence units defined - only the first persistence unit will be recognized
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;
}
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);
}
}
Dao辅助例子
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 指出的那样,错误消息实际上指示了一个没有 unitName 的注入点(我在源代码中忘记了这一点...)一旦我摆脱了它,一切都会按预期进行。当我实际上在 jboss AS 中发现一些旧问题时,我在谷歌搜索这个问题时也有点困惑,这实际上似乎是一个问题,一次。
在 persistence.xml 中为持久性单元添加此选项解决了我的问题:
<property name="wildfly.jpa.default-unit" value="true"/>
是否可以在 Wildfly (9.0.2) 应用程序中有两个持久性单元?
我得到 "WFLYJPA0061: Persistence unitName was not specified and there are 2 persistence unit definitions in application deployment deployment "jasper-web.war”。要么将应用程序部署更改为只有一个持久性单元定义,要么为每个对持久性单元的引用指定 unitName。”
我在 @PeristenceContext
注释中指定了 unitName
。我在某个可以禁用的地方阅读
<!--
<subsystem xmlns="urn:jboss:domain:jpa:1.1">
<jpa default-datasource="" default-extended-persistence-inheritance="DEEP"/>
</subsystem>
-->
在 standalone.xml
中。这删除了错误消息,但也禁用了 entityManagers 的注入(在代码中引用它们的空指针)
我试图将持久性单元拆分到两个不同的 ejb-jars,每个都有自己的 persistence.xml,但由于它们仍然包含在同一个 war 中,Wildfly 仍然抱怨。
这两个持久化单元与hibernate 一起使用,一个与postgresql 数据库一起使用,另一个与用于ms 访问的ucanaccess 驱动程序一起使用。他们都分开工作。
这里是一个在我们的 wildfly 8.x/9.x ejb app 中工作的例子:
首先为 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 缺陷):
Multiple persistence units defined - only the first persistence unit will be recognized
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;
}
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);
}
}
Dao辅助例子
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 指出的那样,错误消息实际上指示了一个没有 unitName 的注入点(我在源代码中忘记了这一点...)一旦我摆脱了它,一切都会按预期进行。当我实际上在 jboss AS 中发现一些旧问题时,我在谷歌搜索这个问题时也有点困惑,这实际上似乎是一个问题,一次。
在 persistence.xml 中为持久性单元添加此选项解决了我的问题:
<property name="wildfly.jpa.default-unit" value="true"/>