我在persistence.xml中有以下内容
<persistence-unit name="test" transaction-type="RESOURCE_LOCAL">
<class>com.merc.model.log.EventLogging</class>
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
<properties>
<!-- Scan for annotated classes and Hibernate mapping XML files -->
<property name="hibernate.archive.autodetection" value="class"/>
</properties>
</persistence-unit>
如果我注释掉 com.merc.model.log.EventLogging,我会收到未知实体异常。
关于为什么自动检测不起作用的任何想法
最佳答案
这可能是由于默认情况下自动检测适用于同一类路径项中的类,其中 persistence.xml
位于。
因此,对于代码本身和测试(例如,如果您使用具有默认配置的 Maven),您有单独的目标文件夹,并且如果此 persistence.xml
编译后最终在测试的目标文件夹中,不会检测到主目标文件夹中的类。
您可以使用 <jar-file>
元素指向应该搜索实体的其他类路径项。
如果您使用 Maven,您可以使用资源过滤以优雅的方式完成:persistence.xml
:
<jar-file>${project.build.outputDirectory}</jar-file>
pom.xml
:<build>
<testResources>
<testResource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</testResource>
</testResources>
</build>
关于jpa - 休眠 jpa 单元测试自动检测不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4726521/