OK, sorry, I have been looking for answers to this for several hours, but I needed to enter the whole question so that StackOverflow would launch the link I was looking for. Here you can read a lot of important information.
I have a Spring project created using Spring Roo to use Hibernate and MySQL. However, for testing, I want to use HSQLDB in memory, because Roo integration tests delete data with identifiers (primary keys) from 0 to 10 (instead of deleting data using the assigned database identifiers for the data that they have already created), which means deletes data that is already in the database, which in my case causes a violation of the restriction before it turns around to cancel the transaction.
This is a bit more complicated, because I switch entire database providers, which means different dialects of hibernation, as well as different DDL settings (validation in production, creation-fall in the test). But it does not work, as I expect, and I am at a standstill why.
If you know why it does not work, say so, even if you do not have a solution.
This is a Roo project, I am using Maven, of course. So, the first thing I tried was the test file src/test/resources/META-INF/persistence.xml
, as well as the test file src/test/resources/META-INF/spring/database.properties
. This did not work, because when mvn test
started, everything broke, and the corresponding message was
Conflicting persistence unit definitions for name 'persistenceUnit'
Why was mvn test
still collecting non-test resources?
So, I renamed src/test/resources/META-INF/spring
to spring-test
and copied applicationContext.xml
into it. I changed the context configuration in the test classes to
@ContextConfiguration(locations = "classpath:/META-INF/spring-test/applicationContext*.xml")
Having completed (or so I thought) the separation, I made a couple of corrections to spring-test/applicationContext.xml
:
The path to the property files has been changed:
<context:property-placeholder location="classpath*:META-INF/spring/*.properties"/>
to
<context:property-placeholder location="classpath*:META-INF/spring-test/*.properties"/>
Changed unit name:
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory"> <property name="persistenceUnitName" value="persistenceUnit"/> <property name="dataSource" ref="dataSource"/> </bean>
to
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory"> <property name="persistenceUnitName" value="testPersistenceUnit"/> <property name="dataSource" ref="dataSource"/> </bean>
And I made a corresponding change in the name of the save unit to src/test/resources/META-INF/persistence.xml
Well, well, now there is no conflict, but for some reason Hibernate has lost entity mappings (for example, for the Product
object), and I get:
org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.hql.ast.QuerySyntaxException: Product is not mapped [SELECT o FROM Product o];
Why has Spring / Hibernate lost object mappings in this configuration?
So, I tried to merge two persistence.xml
files so that one file in src/main/resources/META-INF
included as a unit of conservation.
What works!!??
I think this is ugly, because now I have a test configuration in my production code, but this is what I get with.
What's better?
As far as I understand, the properties in the persistence.xml file are not available, as they are inside the Spring XML files. Therefore, I donβt think I can do what I want with just a test properties file.
Ideally, I would run tests using the entire configuration in src / main / resources, except that it is specifically overridden in src / test / resources. Is there any way to do this?
Thanks for any information you can provide!