有没有办法扫描JPA实体不在persistence.xml文件中声明持久化类? [英] Is there a way to scan JPA entities not to declare persistent classes in a persistence.xml file?

查看:133
本文介绍了有没有办法扫描JPA实体不在persistence.xml文件中声明持久化类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to take advantage of JPA @Entity annotations not to declare class entities a J2SE persistence.xml file. What I'd like to avoid :

<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>

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

and here is what my actual persistence.xml look alike

    <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>

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

Is there a standard way to scan JPA entities in a persistence.xml file from within a JAR module? Is there a unstandard Hibernate way to scan JPA entities in a persistence.xml file from within a JAR module?

推荐答案

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

-Make sure your entities and persistence.xml end up in the same classpath when you build your jar.

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

The entities cannot be scanned if they are sitting in another classpath. If you need to have them sitting in different classpaths, heres one trick I've seen to make it work: No autodetection of JPA Entities in maven-verify.

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

If you are unsure where things are ending, you can unzip the .jar file and peak. This is an unpacked persistence web project:

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

Notice my classes are down the com directory, and my persistence.xml is in the META-INF directory. Both are in the same 'classes' classpath, so autoscan will work.

-Set hibernate.archive.autodetection property。

-Set hibernate.archive.autodetection property.

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

- 对持久性单位添加false

-Add false to the persistence-unit

<persistence-unit name=...>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
    ...

希望其中一个适合你。

这篇关于有没有办法扫描JPA实体不在persistence.xml文件中声明持久化类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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