一个 Persistence.xml 中有两个持久单元

发布于 2024-10-25 04:04:01 字数 1626 浏览 2 评论 0原文

我们创建了一些我们所有项目都会使用的库,这些库将提供我们所有系统的基本功能(登录、一些管理等)。但应用程序本身可以使用另一个数据库。

我们所做的是创建具有两个持久单元的 Persistence.xml。并将所有核心库实体打包在名为“LN-model.jar”的 jar 中,并将测试应用程序的所有实体打包在“App-model.jar”中。但由于某种原因,我们仍然收到以下消息。

无法解析名为 [gfdeploy#/Users/zkropotkine/WORK/SeguridadCore/dist/gfdeploy/SeguridadCore 的模块范围内与 persistence-context-ref-name [xxxxlistener.InicializadorListener/em] 对应的持久性单元-war_war]。请验证您的申请。

这是我们的 Persistence.xml

<persistence version="1.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_1_0.xsd">

<persistence-unit name="x" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>jdbc/x</jta-data-source>
    <jar-file>App-model.jar</jar-file>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
    </properties> 
</persistence-unit>

<persistence-unit name="y" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>jdbc/y</jta-data-source>
    <jar-file>LN-model.jar</jar-file>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties/>
</persistence-unit> 

顺便说一句,我们将 Persistence.xml 放入 jar 中,并将其添加到企业项目 (EAR) 中。

We created some libraries that all our projects will use, this libraries will provide the basic functionality of all our systems (login, some manage, etc). But the application itself could use another database.

What we did was to create the Persistence.xml with two persist units. And package all the core library entities in a jar called "LN-model.jar" and all of the entities of out test app in "App-model.jar". But for some reason we still obtain the following message.

Could not resolve a persistence unit corresponding to the persistence-context-ref-name [x.x.x.x.listener.InicializadorListener/em] in the scope of the module called [gfdeploy#/Users/zkropotkine/WORK/SeguridadCore/dist/gfdeploy/SeguridadCore-war_war]. Please verify your application.

Here's our Persistence.xml

<persistence version="1.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_1_0.xsd">

<persistence-unit name="x" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>jdbc/x</jta-data-source>
    <jar-file>App-model.jar</jar-file>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
    </properties> 
</persistence-unit>

<persistence-unit name="y" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>jdbc/y</jta-data-source>
    <jar-file>LN-model.jar</jar-file>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties/>
</persistence-unit> 

By the way we put the Persistence.xml in a jar, that we add to our Enterprise Project (EAR).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

二货你真萌 2024-11-01 04:04:01

问题在于 JPA 不知道要使用哪个持久单元。当您只有一个持久性单元时,不会发生此问题。要修复,请执行以下操作:

您需要在 Ejb 中指定一个持久性单元: @PersistenceContext(unitName="...")

The problem is that the JPA does not know which is the persistence unit to use. when you have only one persistence unit this problem does not occur. To fix do the following:

You need to specify a persistence unit : @PersistenceContext(unitName="...") in the Ejb that do not have

怪我闹别瞎闹 2024-11-01 04:04:01

您可以添加注释:

@PersistenceUnit(name = "x")
EntityManagerFactory entityManagerFactory;

@PersistenceContext(unitName = "y")
EntityManager entityManager;

或者您可以手动创建:

EntityManagerFactory emfA = Persistence.createEntityManagerFactory("x", properties);
EntityManagerFactory emfB = Persistence.createEntityManagerFactory("y", properties);

有关更多详细信息,请参阅以下链接: https://docs.oracle.com/html/E25034_01/usingmultipledbs.htm
非常有用,对我很有帮助!

You can add the annotations:

@PersistenceUnit(name = "x")
EntityManagerFactory entityManagerFactory;

@PersistenceContext(unitName = "y")
EntityManager entityManager;

Or you can create it manually:

EntityManagerFactory emfA = Persistence.createEntityManagerFactory("x", properties);
EntityManagerFactory emfB = Persistence.createEntityManagerFactory("y", properties);

For more details, please see the following link: https://docs.oracle.com/html/E25034_01/usingmultipledbs.htm
is very useful, to me helped me!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文