title: Using separate persistence.xml files for production and test with Spring 3.1
body: <p>OK, sorry, I looked for answers to this for hours, but it took me entering the whole question for StackOverflow to bubble up the link I was looking for.  You can read a lot of relevant information <a href="https://stackoverflow.com/questions/385532/how-to-configure-jpa-for-testing-in-maven">here</a>.</p>
  
  <hr>
  
  <p>I have a Spring project created with Spring Roo to use Hibernate and MySQL.  However, for testing, I want to use HSQLDB in memory because the Roo integration tests delete data with IDs (primary keys) 0 through 10 (rather than deleting the data using database assigned ids for data they've already created), which means it deletes data that's already in the database, which in my case causes a constraint violation before it gets around to rolling back the transaction.  </p>
  
  <p>This is a little extra difficult because I'm switching entire database providers which means different Hibernate dialects as well as different DDL settings (validate in production, create-drop in test).  But it is not working like I expect and I'm stumped as to why.</p>
  
  <p><strong>If you know why it's not working, please say so, even if you don't have a solution.</strong></p>
  
  <p>This being a Roo project, I'm using Maven of course. So first thing I tried was having a test-specific <code>src/test/resources/META-INF/persistence.xml</code> file  and likewise a test-specific <code>src/test/resources/META-INF/spring/database.properties</code> file.  That didn't work, as when I ran <code>mvn test</code> everything broke, with the relevant message being</p>
  
  <pre><code>Conflicting persistence unit definitions for name 'persistenceUnit'
  </code></pre>
  
  <p><strong>Why was <code>mvn test</code> still picking up the non-test resources?</strong></p>
  
  <p>So then I renamed <code>src/test/resources/META-INF/spring</code> to <code>spring-test</code> and copied <code>applicationContext.xml</code> into it.  I changed the context configuration in the test classes to </p>
  
  <p><code>@ContextConfiguration(locations = "classpath:/META-INF/spring-test/applicationContext*.xml")</code></p>
  
  <p>Completing (or so I thought) the separation, I made a couple of edits to <code>spring-test/applicationContext.xml</code>:</p>
  
  <p>Changed the path for properties files:</p>
  
  <pre><code>&lt;context:property-placeholder location="classpath*:META-INF/spring/*.properties"/&gt;
  </code></pre>
  
  <p>to</p>
  
  <pre><code>&lt;context:property-placeholder location="classpath*:META-INF/spring-test/*.properties"/&gt;
  </code></pre>
  
  <p>Changed the name of the persistence unit:</p>
  
  <pre><code>&lt;bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory"&gt;
      &lt;property name="persistenceUnitName" value="persistenceUnit"/&gt;
      &lt;property name="dataSource" ref="dataSource"/&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>to</p>
  
  <pre><code>&lt;bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory"&gt;
      &lt;property name="persistenceUnitName" value="testPersistenceUnit"/&gt;
      &lt;property name="dataSource" ref="dataSource"/&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>And I made the corresponding change in persistence unit name to <code>src/test/resources/META-INF/persistence.xml</code></p>
  
  <p>Well, OK, now there's no conflict, but somehow Hibernate has lost the entity mappings (e.g. for the <code>Product</code> entity) and I get: </p>
  
  <pre><code>org.springframework.dao.InvalidDataAccessApiUsageException:
  org.hibernate.hql.ast.QuerySyntaxException: Product is not mapped [SELECT o FROM Product o]; 
  </code></pre>
  
  <p><strong>Why has Spring/Hibernate lost the entity mappings in this configuration?</strong></p>
  
  <p>So next thing I tried was merging the two <code>persistence.xml</code> files so that one file under <code>src/main/resources/META-INF</code> includes both persistence units. </p>
  
  <p><strong>That Works!!??</strong></p>
  
  <p>I think it's ugly, because now I have test configuration in my production code, but it's what I'm getting by with.</p>
  
  <p><strong>What is a better way?</strong></p>
  
  <p>It is my understanding that properties are not available inside persistence.xml the way they are inside the Spring XML files.  So I don't think I can do what I want with just a test-specific properties file.</p>
  
  <p>Ideally I'd run tests using all the configuration under src/main/resources except for what is specifically overridden in src/test/resources.  <strong>Is there a way to accomplish this?</strong></p>
  
  <p>Thanks for any insight you can provide!</p>