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: 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:

Caused by: org.hibernate.MappingException: Named query not known: findPersonNames
    at org.hibernate.internal.AbstractSessionImpl.getNamedQuery( ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.springframework.batch.item.database.HibernateItemReaderHelper.createQuery( ~[spring-batch-infrastructure-3.0.5.RELEASE.jar:3.0.5.RELEASE]
    at org.springframework.batch.item.database.HibernateItemReaderHelper.getForwardOnlyCursor( ~[spring-batch-infrastructure-3.0.5.RELEASE.jar:3.0.5.RELEASE]
    at org.springframework.batch.item.database.HibernateCursorItemReader.doOpen( ~[spring-batch-infrastructure-3.0.5.RELEASE.jar:3.0.5.RELEASE]
    at ~[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

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?
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.
      <!-- here the phase you need -->
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