Hibernate DuplicateMappingException when debugging using JUnit
Hoping someone can help.
My project uses Spring, JPA (hibernate provider), and Hibernate hbm.xml files to map entities and named queries (I like having the mapping externalized). The Spring config is annotation driven. This application starts and runs fine. It also tests fine. All tests run and pass in the Maven runner using the ‘test’ goal. All tests run and pass using the JUnit runner.
The issue is when debugging tests. If I debug using the JUnit Runner, I get the following error:
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource: Invocation of init method failed; nested exception is org.hibernate.DuplicateMappingException: Duplicate query mapping <my-named-query>
I get it when debugging a single test method, a single test class, or all the test classes. But it does not happen when I simply run a test or I run multiple tests. It only happens when debugging. My tests start the Spring context via annotations on the test class.
@ActiveProfiles("test")
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {ApplicationConfiguration.class})
Any thoughts? Thanks in advance.
Please sign in to leave a comment.
Oddly enough... I DO NOT get the issue when I "Debug with JRebel". So odd.
Hello Michael,
Is it possible to provide project example for investigation?
Hi. Thanks for the help. Here is the project example.
https://github.com/michaelajr/debugger-bug
I also included the run configs. Of note, the JUnit Run/Debug config. Works when run, does not work when debugging. Also, I can externally run:
$ mvn -Dmaven.surefire.debug test
And then attach the remote debugger... and it works!
So the issue seems to be debugging using the JUnit Run/Debug config.
Thanks for looking into this!
M
Michael,
Please disable debugger agent in "File | Settings | Build, Execution, Deployment | Debugger | Async Stacktraces"
Wow. That did it. THANK YOU. What was that agent providing? Is there a reason I would want to use it? Thanks so much for the help.
Michael,
It was added to debug asynchronous code: https://www.jetbrains.com/help/idea/tutorial-java-debugging-deep-dive.html#async_stacktraces
It might result in code duplication which caused the error.
Interesting. Ok. Thanks again.
Should I open an issue for this?
I was able to reproduce it on java 9. Interestingly, it works well with java 10. Will investigate more.
This is a bug https://bugs.openjdk.java.net/browse/JDK-8185540 in jdk 9, it is fixed in jdk 10. Please use java 10 or disable the instrumenting agent.