I am not sure what the most appropriate way to handle JPA objects that are shared using a JAR file to multiple projects. ie If you have multiple projects that all have an Employee and Contact and Address objects, and you want to put them in a JAR file, how does this work. ie
Employee.jar
jpa/EMF.class
jpa/Employee.class
jpa/Contact.class
jpa/Address.class
jpa/Organisation.class
META-INF/persistence.xml
Project 1
src/jpa/EMF.java
src/jpa/BlogEntry.java
src/jpa/Comment.java
src/META-INF/persistence.xml
lib/Employee.jar
Project 2
src/jpa/EMF.java
src/jpa/Stock.java
src/jpa/Transaction.java
src/META-INF/persistence.xml
lib/Employee.jar
How does this work, is it ok to have two persistence.xml files like above? ie One for describing the JPA classes in the Employee.jar file, and another one for the project specific JPA classes?
Also, the Employee.jar file should not specify the JDBC url, as each project will be using a dfiferent database or schema. If the JDBC connection properties are only specified in the projects src/META-INF/persistence.xml will the Employee.jar classes pick up those settings?
Of course if is ok to have multiple
persistence.xmlfiles if you’re working with different.jarfiles defining each a different persistence unit. Your persistence provider should be able to figure out which class belongs to which persistence unit, and load the appropriate configuration files for each.Of course, depending on which persistence provider you’re using and in what environment you’re working you might get into further trouble, but that’s another issue however.
But the general idea remains the same, every
.jarfile containing persistent entities should have that set of entities associated as a persistence unit using thepersistence.xmlfile under theclasses/META-INFdirectory.Regards,