3

I have a jar file that cares about all persistence logic. This jar file contains persistence.xml file with all configurations needed. My war uses this jar file as dependency but should not know how it's persisted in database. I have some questions:

1) My WAR have @Entity classes; Do I need to create a persistence.xml in "resources/META-INF"? I already have this in the JAR file.

2) If I need to create another persistence.xml in the WAR, how can I inherite this persistence.xml from the jar-file?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Ronaldo Lanhellas
  • 2,975
  • 5
  • 46
  • 92
  • 3
    If the jar cares about all persistence logic, all entities should be in that jar. Architecturally it makes little sense for "some" entities to live inside your war only, apparently you want all of them to be part of one and the same persistence unit. – Gimby Oct 04 '16 at 12:26
  • 1
    I haven't done it, but generally files are searched for in the classpath in a specific order that you can get by calling `((java.net.URLClassLoader) getClass().getClassLoader()).getURLs()` if that classloader inherits URLClassLoader, which in a webapp, it almost certainly does. It definitely does in Tomcat. When you try loading a resource (namely `META-INF/persistence.xml`) It will stop at the first classpath in which it finds the file. That SHOULD be your WAR file. If it exists later on in a JAR file, it will likely be ignored. – coladict Oct 04 '16 at 12:56
  • So, if i have a persistence.xml in jar-file i don't need another in war file, correct ? – Ronaldo Lanhellas Oct 04 '16 at 13:18
  • Yes, you don't need it in the war file, but if you do have it there, it will override any other `persistence.xml` in the jar files if the provider and unit name are the same. – coladict Oct 04 '16 at 13:27

0 Answers0