Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to reuse class declarations from a persistence.xml?
    primarykey
    data
    text
    <p>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).</p> <p>In order to start creating automated tests for this app using an in-memory database, I had to create an alternative <code>test-persistence.xml</code> listing all the same entities from the default <code>persistence.xml</code>, but specifying alternative <code>orm.xml</code> mapping files that override the schema information from the annotations in the classes.</p> <p>The critical piece in my Spring configuration for tests is like this:</p> <pre><code>&lt;bean id="hsqlFixes" class="com.example.app.util.HSQLFixes" /&gt; &lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" depends-on="hsqlFixes"&gt; &lt;property name="persistenceUnitName" value="puName" /&gt; &lt;property name="persistenceXmlLocation" value="META-INF/persistence-tests.xml" /&gt; .... &lt;/bean&gt; </code></pre> <p><sub>Where <code>HSQLFixes</code> is a bean that executes some JDBC statements just creating some types.</sub></p> <p>And in the <code>persistence-tests.xml</code>:</p> <pre><code>&lt;persistence-unit name="puName" transaction-type="RESOURCE_LOCAL"&gt; &lt;mapping-file&gt;META-INF/orm-tests-db1.xml&lt;/mapping-file&gt; &lt;mapping-file&gt;META-INF/orm-tests-db2.xml&lt;/mapping-file&gt; &lt;mapping-file&gt;META-INF/orm-tests-db3.xml&lt;/mapping-file&gt; ... followed by the same list of entities from persistence.xml </code></pre> <p>Now, I want to get rid of the duplication of the classes listing in <code>persistence.xml</code> and <code>test-persistence.xml</code>, cause I feel I could automate the generation of this second file.</p> <p>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?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload