title: Using different orm.xml configurations in multi-module maven projects
body: <p>I have a set of maven projects defined based on a layered architecture, as follows:</p>
  
  <p>1- domain</p>
  
  <pre><code>-src/main/java
  -src/main/resources/META-INF/
  -src/test/java
  -src/test/resources/META-INF/
  </code></pre>
  
  <p>2- repository (depending on domain)</p>
  
  <pre><code>-src/main/java
  -src/main/resources/META-INF/
  -src/test/java
  -src/test/resources/spring/my-appcontext.xml
  -src/test/resources/META-INF/orm.xml
  </code></pre>
  
  <p>My unit tests in repository layer make use of DbUnit in combination with hsql in-memory database. The persistence configuration is based on JPA/Hibernate, configured through spring. Below an example of the spring configuration I am using in my tests:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;!-- our datasource --&gt;
  &lt;bean id="myDatasource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
    &lt;property name="driverClassName" value="${datasource.driverClassName}" /&gt;
    &lt;property name="url" value="${datasource.url}" /&gt;
    &lt;property name="username" value="${datasource.username}" /&gt;
    &lt;property name="password" value="${datasource.password}" /&gt;
  &lt;/bean&gt;
  
  &lt;!-- let hibernate be in between the database and JPA --&gt;
  &lt;bean id="myJpaVendorAdapter"
     class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"&gt;
     &lt;property name="database" value="${jpa.database}" /&gt;
     &lt;property name="showSql" value="${jpa.showSql}" /&gt;
     &lt;property name="generateDdl" value="${jpa.generateDdl}" /&gt;
     &lt;property name="databasePlatform" value="${jpa.databasePlatform}" /&gt;
  &lt;/bean&gt;
  &lt;!-- entityManagerFactory - CONTAINER-managed --&gt;
  &lt;bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
        p:dataSource-ref="molecularanalysisDatasource"
        p:mappingResources="${jpa.orm}"
        p:packagesToScan="com.mypackages.domain"&gt;
        &lt;property name="jpaVendorAdapter" ref="myJpaVendorAdapter" /&gt;
        &lt;property name="jpaProperties"&gt;
      &lt;props&gt;
        &lt;prop key="hibernate.hbm2ddl.auto"&gt;${hibernate.hbm2ddl.auto}&lt;/prop&gt;
        &lt;prop key="hibernate.showSql"&gt;${hibernate.showSql}&lt;/prop&gt;
        &lt;prop key="hibernate.formatSql"&gt;${hibernate.formatSql}&lt;/prop&gt;
        &lt;prop key="dialect"&gt;${hibernate.dialect}&lt;/prop&gt;
        &lt;prop key="hibernate.connection.driver_class"&gt;${datasource.driverClass}&lt;/prop&gt;
            &lt;prop key="hibernate.connection.url"&gt;${datasource.url}&lt;/prop&gt;
        &lt;prop key="hibernate.connection.username"&gt;${datasource.username}&lt;/prop&gt;
        &lt;prop key="hibernate.connection.password"&gt;${datasource.password}&lt;/prop&gt;
      &lt;/props&gt;
        &lt;/property&gt;
  &lt;/bean&gt;
  &lt;bean id="dbUnitDatabaseConfig"    
     class="com.github.springtestdbunit.bean.DatabaseConfigBean"&gt;
     &lt;property name="qualifiedTableNames" value="true" /&gt;
  &lt;/bean&gt;
  &lt;bean id="dbUnitDatabaseConnection"
     class="com.github.springtestdbunit.bean.DatabaseDataSourceConnectionFactoryBean"&gt;
   &lt;property name="dataSource" ref="myDatasource" /&gt;
   &lt;property name="databaseConfig" ref="dbUnitDatabaseConfig" /&gt;
   &lt;property name="schema" value="${datasource.schema}" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>The orm.xml I am using in the test scope makes sure that tables are created in hsqldb under the default catalog name PUBLIC. In other words using the orm.xml in test scope makes sure my tests do not care about specific catalog/schema names defined on domain entities, but just use the default one under hsqldb (This is because hsql allows only one Catalog, namely PUBLIC) when creating the tables during testing.</p>
  
  <p>When I run my tests using Eclipse, the orm.xml under "src/test/resources/META-INF/orm.xml" can be found and is used by the entitymanagerfactory to create the tables in hsqldb.
  However, if I run the tests using maven, the orm.xml is not picked up and as a result the entitymanager tries to creates my tables (entities) using the real catalog and schema names, which will fail in hsqldb since it only has one catalog, namely PUBLIC. </p>
  
  <p>After enabling debugging in spring I noticed that the reason for this is because entitymanagerfactory uses a different "root Url" to search for configuration files when configuring the Jpa/Hibernate and persistence objects. </p>
  
  <p>In case of running tests through Eclipse, entity manager uses the "target/test-classes/" directory where the META-INF/orm.xml can be found. In case of running tests through Maven the entity manager uses the domain.jar, where the META-INF/orm.xml is not included (since this is only defined in tests within integration module). Below the debug info:</p>
  
  <p>1- Running tests through Eclipse:</p>
  
  <pre><code> [main] DEBUG - Processing PersistenceUnitInfo [
      name: default
      persistence provider classname: null
      classloader: sun.misc.Launcher$AppClassLoader@35a16869
      Temporary classloader:
          org.springframework.instrument.classloading.SimpleThrowawayClassLoader@706f2039
           excludeUnlistedClasses: true
           JTA datasource: null
           Transaction type: RESOURCE_LOCAL
           PU root URL: file:/C:/DEVELOPMENT/project/integration/test-classes/
           Shared Cache Mode: UNSPECIFIED
           Validation Mode: AUTO
           Jar files URLs []
  </code></pre>
  
  <p>2- Running tests through Maven:</p>
  
  <pre><code>[main] DEBUG - Processing PersistenceUnitInfo [
     name: default
      persistence provider classname: null
      classloader: sun.misc.Launcher$AppClassLoader@35a16869
      Temporary classloader:
      org.springframework.instrument.classloading.SimpleThrowawayClassLoader@706f2039
      excludeUnlistedClasses: true
      JTA datasource: null
      Transaction type: RESOURCE_LOCAL
      PU root URL: file:/C:/DEVELOPMENT/project/maven-repository/domain/domain.jar
      Shared Cache Mode: UNSPECIFIED
      Validation Mode: AUTO
      Jar files URLs []
  </code></pre>
  
  <p>What I am not able to get working is how to make entitymanager include the META-INF/orm.xml when scanning for orm.xml file. Does anyone know how this can be done in Maven in testing? Adding this orm.xml to domain (src/main/resources/META-INF) will not help since this setting should only be used in test context.</p>