tags:

views:

33

answers:

1

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?

+2  A: 

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?

Of course if is ok to have multiple persistence.xml files if you're working with different .jar files 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 .jar file containing persistent entities should have that set of entities associated as a persistence unit using the persistence.xml file under the classes/META-INF directory.

Regards,

StudiousJoseph