We're working with JPA and are trying to stick to standard spec (and avoiding Hibernate-specific features).
We use one project (let's call it X) inside another project (A) as a Maven dependency.
We need JPA to scan project X for Entities as well as scanning project A.
To that end, we've added a line
<jar-file>lib/X-v5-4.0.jar</jar-file>
inside
<persistence-unit>
in persistence.xml. This works fine.
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.
We've come up with a system using Maven resource filtering:
<jar-file>lib/X-v5-${x-version}.jar</jar-file>
in persistence.xml and
<properties>
<x-version>4.0</x-version>
</properties>
and ${x-version} in pom.xml.
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.
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.
Is this possible?
EDIT (OUR SOLUTION):
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.
A code snippet:
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="jpaDataSource" />
<property name="loadTimeWeaver">
<bean
class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
</property>
<property name="packagesToScan">
<list>
<value>org.com.our.external.library.package1</value>
<value>org.com.our.external.library.package2</value>
<value>org.com.our.external.library.package3</value>
</list>
</property>
</bean>
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.
The groupId is a parameter indicating the group or individual that created a project, which is often a reversed company domain name. The artifactId is the base package name used in the project, and we use the standard archetype.
Maven uses a set of identifiers, also called coordinates, to uniquely identify a project and specify how the project artifact should be packaged: groupId – a unique base name of the company or group that created the project. artifactId – a unique name of the project.
Definition. The groupId is an XML element in the POM. XML file of a Maven project that specifies the id of the project group. In contrast, artifactId is an XML element in the POM. XML of a Maven project that specifies the id of the project (artifact).
The persistence. xml configuration file is used to configure a given JPA Persistence Unit. The Persistence Unit defines all the metadata required to bootstrap an EntityManagerFactory , like entity mappings, data source, and transaction settings, as well as JPA provider configuration properties.
I have solved it this way:
in persistence.xml
substitute the whole <jar-file>
tag, i.e.
<persistence-unit name="PersistenceUnit">
...
<!-- here is where the jar file is supposed to go -->
${importjarfile}
...
</persistence-unit>
Finally, set your properties like this:
<properties>
<x-version>4.0</x-version>
<importjarfile><![CDATA[<jar-file>lib/X-v5-${x-version}.jar</jar-file>]]></importjarfile>
</properties>
In this way Eclipse won't see the jar-file
tag and won't complain.
My solution was actually a little bit cleaner since I was using a .properties
file so I didn't have to use CDATA, I hope it still works.
My solution:
importjarfile = <jar-file>lib/X-v5-${x-version}.jar</jar-file>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With