gpt4 book ai didi

java - 多个 jar ,单个持久性单元解决方案?

转载 作者:IT老高 更新时间:2023-10-28 20:48:01 24 4
gpt4 key购买 nike

包括我在内的一些人一直在努力将来自不同模块(jar)的实体合并到一个持久化单元中(尤其是 JavaSE,例如这里的 JPA 2.0: Adding entity classes to PersistenceUnit *from different jar* automatically)。根据答案,没有简单的直接方法可以做到这一点。一种解决方案是在单个持久性单元文件中列出所有 jar 中的所有类,但这并不是很优雅。我可能不小心找到了另一种方法。通常我所有的实体类都使用注解进行映射。至于解决方法:persistence.xml可以包含多个 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>

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

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

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

即使映射文件没有映射任何属性Java 类中的注释仍然会被考虑,一切似乎都正常!这意味着只需包含来自特定 JAR 的 XML 映射文件,就可以轻松地将来自多个 jar 的实体包含到单个持久性单元中。

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

最佳答案

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

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

这里是 JPA 2.0 spec 的摘录:

12.1 Use of the XML Descriptor

... The absence orpresent of the xml-mapping-metadata-complete subelement contained inthe persistence-unit-defaults subelement of the entity-mappingselement controls whether the XML object/relational mapping descriptoris used to selectively override annotation values or whether it servesas a complete alternative to Java language metadata annotations.

Ifthe xml-mapping-metadata-complete subelement is specified, thecomplete set of mapping metadata for the persistence unit is containedin the XML mapping files for the persistence unit, and any persistenceannotations on the classes are ignored.

关于java - 多个 jar ,单个持久性单元解决方案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15026302/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com