有没有办法扫描JPA实体不要在persistence.xml文件中声明持久化类?

1to*_*tox 13 java jpa persistence.xml

我想利用JPA @Entity注释不要将类实体声明为J2SE persistence.xml文件.我想避免的:

<persistence-unit name="test" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>com.mycompany.entities.Class1</class>
    <class>com.mycompany.entities.Class2</class>
    <class>com.mycompany.entities.Class3</class>
</persistence-unit>
Run Code Online (Sandbox Code Playgroud)

这是我的实际persistence.xml看起来很像

    <persistence-unit name="test" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <properties>
        <!-- Scan for annotated classes and Hibernate mapping XML files -->
        <property name="hibernate.archive.autodetection" value="class, hbm" />
        <property name="hibernate.cache.use_second_level_cache" value="false" />
        <property name="hibernate.cache.use_query_cache" value="false" />
        <property name="hibernate.hbm2ddl.auto" value="create-drop" />
    </properties>
</persistence-unit>
Run Code Online (Sandbox Code Playgroud)

是否有一种标准方法可以在JAR模块中扫描persistence.xml文件中的JPA实体?是否有一种不标准的Hibernate方法从JAR模块中扫描persistence.xml文件中的JPA实体?

Cod*_*ody 16

- 确保在构建jar时,实体和persistence.xml最终位于相同的类路径中.

如果实体位于另一个类路径中,则无法扫描这些实体.如果你需要让它们坐在不同的类路径中,那么我已经看到了一个让它工作的技巧:在maven-verify中没有自动检测JPA实体.

如果您不确定结束的位置,可以解压缩.jar文件和峰值.这是一个解压缩的持久性Web项目:

解压缩的Web持久性JAR

请注意我的类在com目录下,我的persistence.xml在META-INF目录中.两者都在相同的"类"类路径中,因此autoscan将起作用.

-Set hibernate.archive.autodetection属性.

<!-- Scan for annotated classes and Hibernate mapping XML files -->
<property name="hibernate.archive.autodetection" value="class, hbm" />
Run Code Online (Sandbox Code Playgroud)

- 将false添加到持久性单元

<persistence-unit name=...>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
    ...
Run Code Online (Sandbox Code Playgroud)

希望其中一个适合你.