Monday, June 20, 2011

Maven Hibernate Spring Configuration

its a good idea to have the tests running against another database.

We can setup 2 persistence units in the persistence.xml one for main and one for test.
The spring context in the test classes then plugs into the other jpa unit.

How to set up?

Trying to create a separate & different persistence.xml in the test resources fails as the annotations in the main source tree still needs a persistence.xml in their visible classpath to enable the orm.
Since the test classpath is not visible to the main java classes this has to stay in the main resources!

Working Config

The working solution turned out to be one main/resources/META-INF/persistence.xml containing the 2 persistence units.
We need a separate test/resources/applicationContext.xml to shadow the main resource version to connect the datasource and the entitymanager Beans with the test persistence unit!


Caveats

Dont forget to enable resource filtering for both resource directories to substitute your config values into all relevant places



org.hibernate.ejb.HibernatePersistence






















org.hibernate.ejb.HibernatePersistence



















No comments: