多个罐子,单个持久性单元解决方案? [英] Multiple jars, single persistence unit solution?

查看:142
本文介绍了多个罐子,单个持久性单元解决方案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

包括我在内的一些人一直在努力将来自不同模块(jar)的实体合并到一个持久性单元中(尤其是 JavaSE ,例如这里JPA 2.0:自动从不同的jar *中添加实体类到PersistenceUnit * )。根据答案,没有简单的直接方法来做到这一点。其中一个解决方案是在单个持久性单元文件中列出所有jar中的所有类,但这并不是很优雅。我可能偶然发现了另一种方式。通常,我的所有实体类都使用注释进行映射。至于解决方案: persistence.xml 可以包含多个XML映射文件,例如:

Some people, including me, have been struggling with merging entities from different modules (jars) into a single persistence unit (especially with JavaSE, for instance here JPA 2.0: Adding entity classes to PersistenceUnit *from different jar* automatically ). Based on the answers there is no easy direct way to do that. One of solutions is to list all classes from all jars in single persistence unit file, but that's not really elegant. I might have accidentally found another way. Generally all my entity classes are mapped using annotations. As for the solution: persistence.xml can include multiple XML mapping files, eg:

main。 jar!META-INF / persistence.xml:

<persistence-unit name="PU" transaction-type="RESOURCE_LOCAL">
  <mapping-file>META-INF/order-mappings.xml</mapping-file>
  <mapping-file>META-INF/customer-mappings.xml</mapping-file>
</persistence-unit>

映射文件可以放在不同的罐子里。我注意到它们可能包含< entity> 元素没有任何属性,例如:

The mapping files can be placed in different jars. What I noticed is that they may contain <entity> elements without any attributes, eg:

order.jar!META-INF / order-mappings.xml

<entity-mappings>       
  <entity class="com.company.Order"></entity>    
</entity-mappings>

即使映射文件没有映射任何属性,也会采用Java类中的注释无论如何考虑到,一切似乎工作得很好!这意味着只需通过包含来自特定JAR的XML映射文件,就可以轻松地将来自多个jar的实体包含到单个持久性单元中。

Even if the mapping file doesn't map any attributes the annotations in Java class are taken into account anyway and everything seems to work just fine! That would mean it is easily possible to include entities from multiple jars into a single persistence unit just by including XML mapping files from particular JARs.

我的问题是:这是一个允许JPA映射文件使用或只是我的持久性提供程序(Hibernate)的副作用?

My question is: is this an allowed JPA mapping file usage or just a side-effect of my persistence provider (Hibernate)?

推荐答案

是的,这是允许的JPA规范。

Yes, this is allowed by the JPA specification.

XML实体映射旨在覆盖JPA注释。
除非您使用< xml-mapping-metadata-complete /> 标记专门更改默认行为,否则JPA提供程序将使用没有XML映射的注释。

XML entity mappings are designed to override JPA annotations. Unless you specifically change the default behavior using <xml-mapping-metadata-complete/> tag, JPA provider will use annotations where there is no XML mapping.

以下是 JPA 2.0规范


12.1使用XML描述符

...
中包含的xml-mapping-metadata-complete子元素缺席或
persistence-unit-defaults实体映射的子元素
元素控制是否使用XML对象/关系映射描述符
来有选择地覆盖注释值,或者它是否将
作为Java语言元数据注释的完全替代。

... The absence or present of the xml-mapping-metadata-complete subelement contained in the persistence-unit-defaults subelement of the entity-mappings element controls whether the XML object/relational mapping descriptor is used to selectively override annotation values or whether it serves as a complete alternative to Java language metadata annotations.

如果
指定了xml-mapping-metadata-complete子元素,则持久单元的
完整映射元数据集包含在$ b中持久性单元的XML映射文件中的$ b,以及类上的任何持久性
注释都将被忽略。

If the xml-mapping-metadata-complete subelement is specified, the complete set of mapping metadata for the persistence unit is contained in the XML mapping files for the persistence unit, and any persistence annotations on the classes are ignored.

这篇关于多个罐子,单个持久性单元解决方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆