我想利用 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>
这就是我实际的 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>
是否有一种标准方法可以从 JAR 模块中扫描 persistence.xml 文件中的 JPA 实体? 是否有一种非标准的 Hibernate 方法可以从 JAR 模块中扫描 persistence.xml 文件中的 JPA 实体?
最佳答案
-确保您的实体和 persistence.xml 在构建 jar 时最终位于相同的类路径中。
如果实体位于另一个类路径中,则无法对其进行扫描。如果您需要让它们位于不同的类路径中,这里是我见过的一个技巧:No autodetection of JPA Entities in maven-verify .
如果您不确定事情在哪里结束,您可以解压缩 .jar 文件并达到顶峰。这是一个未打包的持久性 Web 项目:
请注意,我的类位于 com 目录下,而我的 persistence.xml 位于 META-INF 目录中。两者都在相同的“类”类路径中,因此自动扫描将起作用。
-设置 hibernate.archive.autodetection 属性。
<!-- Scan for annotated classes and Hibernate mapping XML files -->
<property name="hibernate.archive.autodetection" value="class, hbm" />
-给持久化单元添加false
<persistence-unit name=...>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
...
希望其中之一对您有用。
关于java - 有没有办法扫描 JPA 实体而不在 persistence.xml 文件中声明持久类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17951297/