繁体   English   中英

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

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

这个问题很难解释,所以请看一下项目: https : //github.com/darzz/boot_bug这是最小的设置,重现了错误。

描述:应用程序堆栈是带有 Spring Data 和 Spring Batch 的 Spring Boot。 src/main/resources/queries下有testNamedQuery.hbm.xml文件。

应用程序类运行时,批处理作业成功完成,日志中没有异常。 但是,当从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

所以看起来在运行测试时,*.hbm.xml 文件没有加载! 经过研究和调试,我想,我可能已经找到了原因——持久化单元根 url 被设置为测试目标/测试类,但映射文件在 /target/classes 中。

我认为可能的原因可能与此处描述的类似http://blog.carbonfive.com/2007/05/17/using-classpath-vs-classpath-when-loading-spring-resources/

但是我不知道如何在 Spring Boot 中解决这个问题,而不是为了测试目的而创建 persistence.xml 配置。 也不想将 *.hbm.xml 文件从 main/resources 复制到 test/resources。

有没有人有想法?

@Autowired
private ResourceLoader rl;


@Bean
public LocalSessionFactoryBean sessionFactory() throws IOException {
    LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
    sessionFactoryBean.setMappingLocations(loadResources());
    return sessionFactoryBean;
}

public Resource[] loadResources() {
    Resource[] resources = null;
    try {
        resources = ResourcePatternUtils.getResourcePatternResolver(rl)
            .getResources("classpath:/hibernate/*.hbm.xml");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return resources;
}

如果我正确理解了问题,您想运行 Spring Boot Test,它应该从类路径中获取 *.hbm 文件。

你把 *.hbm 文件放在哪里?

您必须确保所有 *.hbm 文件都保存在 src/main/resources 下。 因此,当您运行测试类时,它将调用引用来自 src/main/resources 的 *.hbm 文件的实际类。

如果上述解决方案没有帮助,那么您需要共享您的项目文件结构。

我查看了 git 项目。 似乎您需要更改 ApplicationNotWorking.java 因为它是一个测试类,因此应该如下所示:

//源代码/测试/Java

package bug;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.stackoverflow.springboot.BatchProcessing;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class )
public class ApplicationNotWorking {
    //Inject required bean which you want to test.
    @Autowired
    private BatchProcessing bProcess;
    //If above @Autowired BatchProcessing  do not work then you can object  
    //directly to kick off the test. BatchProcessing bProcess = new 
    //BatchProcessing();    

    //This way you can test each method
    @Test
    public void testBatchProcessing(){
        System.out.println("***BatchProcessing: " + bProcess.batchProcess());
    }
}

暂无
暂无

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

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