Java 有没有一种方法可以扫描JPA实体而不在persistence.xml文件中声明持久类?
我想利用JPA@Entity注释不将类实体声明为J2SE persistence.xml文件。我想避免的是:Java 有没有一种方法可以扫描JPA实体而不在persistence.xml文件中声明持久类?,java,jpa,persistence.xml,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&l
<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>
org.hibernate.ejb.HibernatePersistence
com.mycompany.entities.Class1
com.mycompany.entities.Class2
com.mycompany.entities.Class3
下面是我实际的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>
org.hibernate.ejb.HibernatePersistence
是否有一种标准方法可以从JAR模块中扫描persistence.xml文件中的JPA实体?
是否有一种非标准的Hibernate方法可以从JAR模块中扫描persistence.xml文件中的JPA实体?-在构建JAR时,确保实体和persistence.xml最终位于同一类路径中 如果实体位于另一个类路径中,则无法对其进行扫描。如果您需要将它们放在不同的类路径中,下面是我见过的一个使其工作的技巧: 如果您不确定事情将在哪里结束,您可以解压缩.jar文件并使用peak。这是一个未打包的持久性web项目: 注意,我的类位于com目录下,persistence.xml位于META-INF目录下。两者都在相同的“类”类路径中,因此autoscan可以工作 -设置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>
...
假的
...
希望其中一个对你有用。是的,看起来是这样,但在我的情况下不起作用。下面是属性Hibern.Sovi.AutoDebug的定义:“在分析.PAR存档时,确定由Hibernate实体管理器自动发现哪个元素。(默认为类,HBM)。”但是什么是PAR档案?我从来没有听说过这样的AHIVE。PAR归档是持久存档文件,其中可以将实体和持久性.xml捆绑在一起。这不是必需的,所以不用担心(请参阅Hibernate团队成员的最后一篇文章:)。另一个建议是在持久性单元中添加:false。我也更新了我的答案。确保您的类和persistence.xml位于相同的类路径中(请参阅第一个项目符号)。我已经找到了问题的根源。正如您之前警告我的,我正在测试的实体与测试类不在同一类路径中。javadoc中记录的属性: