简体   繁体   English

* .hbm.xml映射文件未在测试中加载

[英]*.hbm.xml mapping files not loaded in test

Please have a look at the project: https://github.com/darzz/boot_bug This is the minimal setup, that reproduces the bug. 请看一下项目: https : //github.com/darzz/boot_bug这是最小化的设置,它再现了该错误。

Description : The application stack is Spring Boot with Spring Data and Spring Batch. 描述 :应用程序堆栈是带有Spring Data和Spring Batch的Spring Boot。 There is testNamedQuery.hbm.xml file under src/main/resources/queries . src / main / resources / queries下有testNamedQuery.hbm.xml文件。

When running from Application class the batch job finishes successfully, no exceptions in logs. Application类运行时,批处理作业成功完成,日志中无异常。 However, when running from ApplicationNotWorking class, which is exact copy, just put in test source root, the batch job fails: 但是,当从ApplicationNotWorking类运行时,它是完全相同的副本,仅放在测试源根目录中,批处理作业将失败:

Caused by: org.hibernate.MappingException: Named query not known: findPersonNames
    at org.hibernate.internal.AbstractSessionImpl.getNamedQuery(AbstractSessionImpl.java:177) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.springframework.batch.item.database.HibernateItemReaderHelper.createQuery(HibernateItemReaderHelper.java:146) ~[spring-batch-infrastructure-3.0.5.RELEASE.jar:3.0.5.RELEASE]
    at org.springframework.batch.item.database.HibernateItemReaderHelper.getForwardOnlyCursor(HibernateItemReaderHelper.java:123) ~[spring-batch-infrastructure-3.0.5.RELEASE.jar:3.0.5.RELEASE]
    at org.springframework.batch.item.database.HibernateCursorItemReader.doOpen(HibernateCursorItemReader.java:185) ~[spring-batch-infrastructure-3.0.5.RELEASE.jar:3.0.5.RELEASE]
    at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.open(AbstractItemCountingItemStreamItemReader.java:144) ~[spring-batch-infrastructure-3.0.5.RELEASE.jar:3.0.5.RELEASE]
    ... 39 common frames omitted

So it looks like that when running tests, *.hbm.xml files are not loaded! 因此 ,在运行测试时,*。h​​bm.xml文件未加载! After research and debugging, I think, I might have found the cause - persistence unit root url is set for tests to target/test-classes, but the mapping files are in /target/classes. 经过研究和调试后,我认为可能已经找到了原因-为目标/测试类的测试设置了持久性单元根url,但是映射文件位于/ target / classes中。

Possible cause in my opinion may be similar to what's described in here http://blog.carbonfive.com/2007/05/17/using-classpath-vs-classpath-when-loading-spring-resources/ 我认为可能的原因可能与以下内容类似:http://blog.carbonfive.com/2007/05/17/using-classpath-vs-classpath-when-loading-spring-resources/

But I have no idea, how to solve this issue in Spring Boot, without creating persistence.xml configuration just for testing purposes. 但是我不知道如何在Spring Boot中解决这个问题,而无需创建persistence.xml配置只是为了测试。 Don't want to copy *.hbm.xml files from main/resources to test/resources either. 也不想将* .hbm.xml文件从main / resources复制到test / resources。

Does anyone have an idea? 有人有主意吗?

if you want to load all the resources/beans during test, you should write test in different way than you have 如果要在测试期间加载所有资源/ bean,则应该以不同于您的方式编写测试

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@TransactionConfiguration(defaultRollback = true)
@Transactional
public class DummyTest{

    @Test
    public void test(){
       //
  }
}

that should load all the resources with hbm mappings 应该使用hbm映射加载所有资源

尝试在存储库或配置类上添加@EnableJpaRepositories(basePackages="com.example.your.package")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM