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

时间:2015-10-30 13:43:39

标签: java spring hibernate spring-boot hibernate-mapping

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

描述: 应用程序堆栈是Spring Boot with Spring Data和Spring Batch。 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。

有没有人有想法?

3 个答案:

答案 0 :(得分:1)

@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;
}

答案 1 :(得分:0)

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

你在哪里保存* .hbm文件?

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

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

答案 2 :(得分:0)

I looked into the git project. It seems you need to change the ApplicationNotWorking.java since it is a test class hence should be something as below:

//src/test/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());
    }
}