EclipseLink、EntityManager 需要两个持久性单元

发布于 2024-08-10 23:04:45 字数 416 浏览 7 评论 0原文

我有一个 jar 库 A(或 eclipse 中的项目),它有自己的持久性单元(META-INF/persistence.xml)和一些实体类,以及使用该库的另一个项目(B)。在项目B中还有持久化单元和实体类。

在项目 BI 中,需要使用项目 A 和 B 中的两个实体类。但是,如果我将“A”设置为持久性单元名称,则 EntityManager 无法创建命名查询(如果该查询位于项目 B 的实体中)。如果我将“B”设置为持久性,则 EntityManager 无法创建命名查询。单元名称,它无法从项目 A 的实体创建命名查询。错误消息是:

NamedQuery of name: MyEntityName.myQueryName not found.

持久性单元可以以某种方式包含其他持久性单元吗?或者还有其他方法可以解决这个问题吗?

I have one jar library A (or project in eclipse), which has it's own persistence unit (META-INF/persistence.xml) and some entity classes, and another project (B) using this one. In project B there is also persistence unit and entity classes.

In project B I need to use both entity classes from project A and B. But if I set "A" as persistence unit name, EntityManager cannot create named query if this query is in entity from project B. If I set "B" as persistence unit name, it cannot create named queries from entities from project A. Error message is:

NamedQuery of name: MyEntityName.myQueryName not found.

Can persistence units somehow include other persistence units? Or is there any other way to solve this problem?

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

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

发布评论

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

评论(2

郁金香雨 2024-08-17 23:04:45

EclipseLink 2.3 引入了复合持久性单元,它允许您创建一个持久性单元,本质上是仅充当两个或多个实际持久性单元的容器。然后,您就可以在应用程序中使用这一单一复合持久性单元,就好像您只有一个持久性单元一样。这应该可以满足您保持 persistence.xml 文件干净以便轻松将模型同步到数据库的目标。非常酷的东西。

EclipseLink 2.3 introduced Composite Persistence Units, which allows you to create a persistence unit that essentially acts only as a container for two or more actual persistence units. You are then able to use this single composite persistence unit in your application as if you had only one persistence unit. This should meet your goals of keeping your persistence.xml files clean for easy synchronization of your model to database. Pretty cool stuff.

守护在此方 2024-08-17 23:04:45

您可以在一个持久单元中列出 A 所需的类,并在另一个持久单元中列出 B 所需的类:

<persistence ...>
    <persistence-unit name="projectA" ...>
        ....
        <class>a.Class1</class>
        <class>a.Class2</class>
        <class>a.Class3</class>
    </persistence-unit>

    <persistence-unit name="projectB" ...>
        ...
        <class>a.Class1</class>
        <class>a.Class2</class>
        <class>a.Class3</class>
        <class>b.Class1</class>
        <class>b.Class2</class>
        <class>b.Class3</class>
    </persistence-unit>
</persistence>

或者,您可以使用 元素,引用 JPA 规范 (6.2.1.6):“如果指定,将搜索这些 JAR 文件以查找托管持久性类,并且将处理在它们上找到的任何映射元数据注释,或者将使用映射注释来映射它们此规范定义的默认值是相对于持久性单元的根指定的(例如,utils/myUtils.jar)。

<persistence ...>
    <persistence-unit name="projectA" ...>
        ...
        <jar-file>relative/path/to/your/library.jar</jar-file>
    </persistence-unit>
</persistence>

You can list your classes needed by A in one persistence unit, and classes needed you B in an other:

<persistence ...>
    <persistence-unit name="projectA" ...>
        ....
        <class>a.Class1</class>
        <class>a.Class2</class>
        <class>a.Class3</class>
    </persistence-unit>

    <persistence-unit name="projectB" ...>
        ...
        <class>a.Class1</class>
        <class>a.Class2</class>
        <class>a.Class3</class>
        <class>b.Class1</class>
        <class>b.Class2</class>
        <class>b.Class3</class>
    </persistence-unit>
</persistence>

Alternatively, you can use the <jar-file> element, quoting from the JPA spec (6.2.1.6): "If specified, these JAR files will be searched for managed persistence classes, and any mapping metadata annotations found on them will be processed, or they will be mapped using the mapping annotation defaults defined by this specification. Such JAR files are specified relative to the root of the persistence unit (e.g., utils/myUtils.jar)."

<persistence ...>
    <persistence-unit name="projectA" ...>
        ...
        <jar-file>relative/path/to/your/library.jar</jar-file>
    </persistence-unit>
</persistence>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文