Click here to Skip to main content
15,609,235 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
The problem is hard to explain, so please have a look at the project: https://github.com/darzz/boot_bug This is the minimal setup, that reproduces the bug.

Description: The application stack is Spring Boot with Spring Data and Spring Batch. There is testNamedQuery.hbm.xml file under src/main/resources/queries.

When running from Application class the batch job finishes successfully, no exceptions in logs. However, when running from ApplicationNotWorking class, which is exact copy, just put in test source root, the batch job fails:

Java
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! 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.

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/

But I have no idea, how to solve this issue in Spring Boot, without creating persistence.xml configuration just for testing purposes. Don't want to copy *.hbm.xml files from main/resources to test/resources either.

Does anyone have an idea?
Posted
Updated 9-Feb-22 6:23am

1 solution

If you use maven, perhaps you could add a copy-resources plugin to copy the hbm files to the test-classes folder.
XML
 <plugin>
  <artifactId>maven-resources-plugin</artifactId>
  <version>3.2.0</version>
  <executions>
    <execution>
      <id>copy-resources</id>
      <!-- here the phase you need -->
      <phase>validate</phase>
      <goals>
        <goal>copy-resources</goal>
      </goals>
      <configuration>
        <outputDirectory>${basedir}/target/test-classes</outputDirectory>
        <resources>
          <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
          </resource>
        </resources>
      </configuration>
    </execution>
  </executions>
</plugin>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900