eclipse 一个persistence.xml中的多个persistence-unit标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8398393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 20:45:06  来源:igfitidea点击:

Multiple persistence-unit tags in one persistence.xml

eclipsehibernatejpa-2.0persistence.xml

提问by artaxerxe

I'm working now with a JPA 2.0 project (in Eclipse Indigo). In my persistence.xml file I need to specify two <persistence-unit> tags, because I have two different database to work with. Doing this, I get the following warning:

我现在正在处理一个 JPA 2.0 项目(在 Eclipse Indigo 中)。在我的 persistence.xml 文件中,我需要指定两个 <persistence-unit> 标记,因为我有两个不同的数据库要使用。这样做,我收到以下警告:

Multiple persistence units defined - only the first persistence unit will be recognized

定义了多个持久性单元 - 仅识别第一个持久性单元

Even more, if I create my entities and put it in my second <persistence-unit>, I get an error saying that my entity isn't declared in a persistence-unit:

更重要的是,如果我创建我的实体并将其放在我的第二个 <persistence-unit> 中,我会收到一条错误消息,指出我的实体未在持久性单元中声明:

Class "my.package.MyClass" is mapped, but is not included in any persistence unit

类“my.package.MyClass”已映射,但不包含在任何持久性单元中

So, the question is: is possible to declare (and use them as expected) many <persistence-unit> tags in persistence.xml file? If yes, what do I need to do for getting the accurate behavior?

所以,问题是:是否可以在persistence.xml 文件中声明(并按预期使用)许多<persistence-unit> 标签?如果是,我需要做什么才能获得准确的行为?

Thanks!

谢谢!

回答by Mikko Maunu

JPA does not limit number of persistence units defined in persistence.xml. This warning just tells about limitation of tool in Eclipse IDE. This is told for example here http://www.eclipse.org/webtools/dali/gettingstarted.php:

JPA 不限制persistence.xml 中定义的持久性单元的数量。此警告仅说明 Eclipse IDE 中工具的限制。例如这里是这样说的http://www.eclipse.org/webtools/dali/gettingstarted.php

Currently Dali only supports one Persistence Unit and one Persistence XML file per project. Other configurations can exist in a JPA project, but the validation and defaults processing may not be correct when multiple persistence units are used.

目前 Dali 每个项目仅支持一个 Persistence Unit 和一个 Persistence XML 文件。JPA 项目中可以存在其他配置,但是当使用多个持久性单元时,验证和默认值处理可能不正确。

I guess there is no known way to get this work in Eclipse IDE.

我想没有已知的方法可以在 Eclipse IDE 中完成这项工作。

回答by VH-NZZ

While this is JPA valid, you may also turn off the resulting (annoying) error notifications on a per-project basis by going into

虽然这是 JPA 有效的,但您也可以通过进入每个项目来关闭由此产生的(烦人的)错误通知

Project Properties => JPA => Errors/Warnings

Activate Enable project specific settingsand under Type, change the notification level for Class is mapped but is not in a persistence unitfrom Errorto anything else you're comfortable with.

激活Enable project specific settingsType,对改变通知的等级Class is mapped but is not in a persistence unit,从Error其他任何事情你满意。

Update for Eclipse Luna:

Eclipse Luna 更新:

The location and name of the setting has changed in Luna. The new location is:

Luna 中设置的位置和名称已更改。新地点是:

Project Properties => JPA => Errors/Warnings

Check Enable project specific settings

查看 Enable project specific settings

Expand Projectgroup

展开Project

Change Multiple persistence units defined:to Ignore.

更改Multiple persistence units defined:Ignore

enter image description here

在此处输入图片说明