gpt4 book ai didi

java - 在 SE 环境中将 JPA 实体 jar 与 persistence.xml 分离

转载 作者:搜寻专家 更新时间:2023-10-30 20:00:56 25 4
gpt4 key购买 nike

有没有一种方法可以在多个 SE 应用程序中重用带有 JPA 注释实体的 jar(作为依赖项)? SE 环境不支持 persistence.xml 中的 ,还有其他方法吗?

最佳答案

官方(根据规范),您必须使用 class 元素指定所有类。引用 JSR-2206.2.1.6 mapping-file, jar-file, class, exclude-unlisted-classes 一章:

A list of named managed persistence classes may also be specified instead of, or in addition to, the JAR files and mapping files. Any mapping metadata annotations found on these classes will be processed, or they will be mapped using the mapping annotation defaults. The class element is used to list a managed persistence class. A list of all named managed persistence classes must be specified in Java SE environments to insure portability. Portable Java SE applications should not rely on the other mechanisms described here to specify the managed persistence classes of a persistence unit. Persistence providers may also require that the set of entity classes and classes that are to be managed must be fully enumerated in each of the persistence.xml files in Java SE environments.

现在,如果您不介意不可移植,Hibernate supports在 Java SE 中使用 jar-file 元素(在这种情况下需要一个绝对 url,不方便)。即使在 JSE 中,Hibernate 实际上也支持自动检测。好多了:

<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="foo">

<!-- This is required to be spec compliant, Hibernate however supports auto-detection even in JSE. -->
<class>foo.Bar<class>

<properties>
<!-- Scan for annotated classes and Hibernate mapping XML files -->
<property name="hibernate.archive.autodetection" value="class, hbm"/>
...
</properties>
</persistence-unit>

</persistence>

关于java - 在 SE 环境中将 JPA 实体 jar 与 persistence.xml 分离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2622424/

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