Some people, including me, have been struggling with merging entities from different modules (jars) into a single persistence unit (especially with JavaSE, for instance here JPA 2.0: Adding entity classes to PersistenceUnit *from different jar* automatically ). Based on the answers there is no easy direct way to do that. One of solutions is to list all classes from all jars in single persistence unit file, but that's not really elegant. I might have accidentally found another way. Generally all my entity classes are mapped using annotations. As for the solution: persistence.xml
can include multiple XML mapping files, eg:
main.jar!META-INF/persistence.xml:
<persistence-unit name="PU" transaction-type="RESOURCE_LOCAL">
<mapping-file>META-INF/order-mappings.xml</mapping-file>
<mapping-file>META-INF/customer-mappings.xml</mapping-file>
</persistence-unit>
The mapping files can be placed in different jars. What I noticed is that they may contain <entity>
elements without any attributes, eg:
order.jar!META-INF/order-mappings.xml
<entity-mappings>
<entity class="com.company.Order"></entity>
</entity-mappings>
Even if the mapping file doesn't map any attributes the annotations in Java class are taken into account anyway and everything seems to work just fine! That would mean it is easily possible to include entities from multiple jars into a single persistence unit just by including XML mapping files from particular JARs.
My question is: is this an allowed JPA mapping file usage or just a side-effect of my persistence provider (Hibernate)?