我想利用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实体?
发布于 2013-07-31 00:22:40
-Make确保在构建jar时,您的实体和persistence.xml最终位于相同的类路径中。
如果实体位于另一个类路径中,则无法扫描这些实体。如果您需要让它们位于不同的类路径中,这里有一个我见过的技巧:No autodetection of JPA Entities in maven-verify。
如果您不确定事情在哪里结束,您可以解压缩.jar文件并使其达到峰值。这是一个未打包的持久性web项目:

注意,我的类位于com目录下,而我的persistence.xml位于META-INF目录中。两者都在相同的“classes”类路径中,因此autoscan将会工作。
-Set hibernate.archive.autodetection属性。
<!-- Scan for annotated classes and Hibernate mapping XML files -->
<property name="hibernate.archive.autodetection" value="class, hbm" />持久性单元的-Add为false
<persistence-unit name=...>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
...希望其中的一个能为你工作。
https://stackoverflow.com/questions/17951297
复制相似问题