I am working on a project that uses JPA/HIbernate and Spring for persistence and transaction management. This application has tables mapped from several catalogs and schemas from the same database server (a Sybase environment).
In order to start creating automated tests for this app using an in-memory database, I had to create an alternative test-persistence.xml
listing all the same entities from the default persistence.xml
, but specifying alternative orm.xml
mapping files that override the schema information from the annotations in the classes.
The critical piece in my Spring configuration for tests is like this:
<bean id="hsqlFixes" class="com.example.app.util.HSQLFixes" />
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
depends-on="hsqlFixes">
<property name="persistenceUnitName" value="puName" />
<property name="persistenceXmlLocation"
value="META-INF/persistence-tests.xml" />
....
</bean>
Where HSQLFixes
is a bean that executes some JDBC statements just creating some types.
And in the persistence-tests.xml
:
<persistence-unit name="puName" transaction-type="RESOURCE_LOCAL">
<mapping-file>META-INF/orm-tests-db1.xml</mapping-file>
<mapping-file>META-INF/orm-tests-db2.xml</mapping-file>
<mapping-file>META-INF/orm-tests-db3.xml</mapping-file>
... followed by the same list of entities from persistence.xml
Now, I want to get rid of the duplication of the classes listing in persistence.xml
and test-persistence.xml
, cause I feel I could automate the generation of this second file.
How could I go to do it dynamically, so that I don't need to change both XML files when I add/remove an entity?