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

Java 测试中未加载Spring Boot*.hbm.xml映射文件,java,spring,hibernate,spring-boot,hibernate-mapping,Java,Spring,Hibernate,Spring Boot,Hibernate Mapping,这个问题很难解释,所以请看一下这个项目: 这是复制错误的最小设置 说明: 应用程序堆栈是带有Spring数据和Spring批处理的Spring引导。 src/main/resources/querys下有testNamedQuery.hbm.xml文件 从应用程序运行时,批处理作业成功完成,日志中没有异常。但是,当从ApplicationNotWorking类运行时(该类是精确副本),仅将其放入测试源根目录中,批处理作业失败: Caused by: org.hibernate.MappingEx

这个问题很难解释,所以请看一下这个项目: 这是复制错误的最小设置

说明: 应用程序堆栈是带有Spring数据和Spring批处理的Spring引导。 src/main/resources/querys下有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中

可能的原因在我看来可能与这里描述的类似

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


有人有什么想法吗?

如果我正确理解了这个问题,您希望运行Spring引导测试,它应该从类路径中提取*.hbm文件

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

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


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

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

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

嗨,谢谢你的回答。我已经创建了一个最小的项目,它复制了这个问题并将其放到github上-请看一看(原始问题已被编辑)。对我来说,这是一些奇怪的东西,但我刚刚开始我的旅程与春季开机…你是做Restful服务?您需要使用Spring引导的集成测试。尽管创建springbootapplication类进行测试;您需要使用@RunWith(SpringJUnit4ClassRunner.class)和@SpringApplicationConfiguration(classes=AppMain.class)创建测试类不,只使用spring批处理应用程序,不需要使用本地数据库进行端到端测试,但是*.hbm.xml文件没有加载,即使我使用了您建议的方法。谢谢您的回答,但这仍然不能解决不加载*.hbm.xml文件的问题。
@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;
}