Note that there are some explanatory texts on larger screens.

plurals
  1. POReferring to the artifactId of a maven dependency jar from persistence.xml
    text
    copied!<p>We're working with JPA and are trying to stick to standard spec (and avoiding Hibernate-specific features).</p> <p>We use one project (let's call it X) inside another project (A) as a Maven dependency.</p> <p>We need JPA to scan project X for Entities as well as scanning project A.</p> <p>To that end, we've added a line </p> <pre><code>&lt;jar-file&gt;lib/X-v5-4.0.jar&lt;/jar-file&gt; </code></pre> <p>inside</p> <pre><code>&lt;persistence-unit&gt; </code></pre> <p>in persistence.xml. This works fine.</p> <p>The issue we still have is that we now need to specify the version of project X in not only pom.xml but also in persistence.xml. This is a recipe for problems with deploys in the future.</p> <p>We've come up with a system using Maven resource filtering:</p> <pre><code>&lt;jar-file&gt;lib/X-v5-${x-version}.jar&lt;/jar-file&gt; </code></pre> <p>in persistence.xml and </p> <pre><code>&lt;properties&gt; &lt;x-version&gt;4.0&lt;/x-version&gt; &lt;/properties&gt; </code></pre> <p>and ${x-version} in pom.xml.</p> <p>This works but is still not perfect, as we'll have to remember to update the version number in a non-standard location each time project X gets a new version.</p> <p>Ideally, we'd like to have a situation where we can adjust version information in the dependency part of pom.xml and changes would propagate to persistence.xml automatically. We'd reduce a lot of possible errors in future deploys this way.</p> <p>Is this possible?</p> <p>EDIT (OUR SOLUTION):</p> <p>we have added a file named jpa.xml. We define an entityManagerFactory, a persistenceAnnotationBeanPostProcessor and a transactionManager in it. The important part here is the entityManagerFactory bean. It has a property "packagesToScan" that allows you to indicate specific packages to scan for entities to put in the persistence context.</p> <p>A code snippet:</p> <pre><code>&lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt; &lt;property name="dataSource" ref="jpaDataSource" /&gt; &lt;property name="loadTimeWeaver"&gt; &lt;bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" /&gt; &lt;/property&gt; &lt;property name="packagesToScan"&gt; &lt;list&gt; &lt;value&gt;org.com.our.external.library.package1&lt;/value&gt; &lt;value&gt;org.com.our.external.library.package2&lt;/value&gt; &lt;value&gt;org.com.our.external.library.package3&lt;/value&gt; &lt;/list&gt; &lt;/property&gt; &lt;/bean&gt; </code></pre> <p>I'm sure you see the advantage: as we are referring to these libraries by package signature, we no longer have to worry about jar version numbers.</p>
 

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