简体   繁体   English

多个罐子,单个持久性单元解决方案?

[英]Multiple jars, single persistence unit solution?

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 ). 包括我在内的一些人一直在努力将来自不同模块(jar)的实体合并到一个持久性单元中(特别是使用JavaSE ,例如JPA 2.0:自动将来自不同jar *的实体类添加到PersistenceUnit * )。 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. 解决方案之一是在单个持久性单元文件中列出所有jar中的所有类,但这并不是很优雅。 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: 至于解决方案: persistence.xml可以包含多个XML映射文件,例如:

main.jar!META-INF/persistence.xml: 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>

The mapping files can be placed in different jars. 映射文件可以放在不同的jar中。 What I noticed is that they may contain <entity> elements without any attributes , eg: 我注意到它们可能包含没有任何属性的 <entity>元素,例如:

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

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

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! 即使映射文件没有映射任何属性, 但无论如何都要考虑Java类中的注释 ,一切似乎都可以正常工作! 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. 这意味着只需通过包含来自特定JAR的XML映射文件,就可以轻松地将来自多个jar的实体包含到单个持久性单元中。

My question is: is this an allowed JPA mapping file usage or just a side-effect of my persistence provider (Hibernate)? 我的问题是:这是一个允许的JPA映射文件用法还是只是我的持久性提供程序(Hibernate)的副作用?

Yes, this is allowed by the JPA specification. 是的,这是JPA规范允许的。

XML entity mappings are designed to override JPA annotations. XML实体映射旨在覆盖JPA注释。 Unless you specifically change the default behavior using <xml-mapping-metadata-complete/> tag, JPA provider will use annotations where there is no XML mapping. 除非您使用<xml-mapping-metadata-complete/>标记专门更改默认行为,否则JPA提供程序将使用没有XML映射的注释。

Here is an excerpt from the JPA 2.0 spec : 以下是JPA 2.0规范的摘录:

12.1 Use of the XML Descriptor 12.1 XML描述符的使用

... 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. ... entity-mappings元素的persistence-unit-defaults子元素中包含的xml-mapping-metadata-complete子元素的缺失或存在控制是否使用XML对象/关系映射描述符来有选择地覆盖注释值或是否它可以作为Java语言元数据注释的完整替代方案。

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. 如果指定了xml-mapping-metadata-complete子元素,则持久性单元的完整映射元数据集将包含在持久性单元的XML映射文件中,并且将忽略类上的任何持久性注释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM