1

I'm using EclipseLink as my JPA implementation. Our project has a directory where we keep all of our config files, and I would like to store my persistence.xml file in the config directory, but cannot find any way to tell createEntityManagerFactory to look there to find the persistence-unit.

Vikdor
  • 23,934
  • 10
  • 61
  • 84
BostonJohn
  • 2,631
  • 2
  • 26
  • 48
  • Quite a strange requirement. Why do you need it? – axtavt Sep 20 '12 at 18:29
  • Management wants all configuration to live in the same place. I can see the utility of that. It doesn't really make sense for a config file to live in jar/war file. – BostonJohn Sep 20 '12 at 18:33

3 Answers3

3

It doesn't answer your question directly, but if you want to do it in order to externalize configuration of the application, I think the best approach would be to extract all configurable properties (such as connection settings, etc) into a separate properties file and pass them to createEntityManagerFactory() as a Map (also note that you can override properties from persistence.xml this way).

Then your persistence.xml will contain only non-configurable settings (such as a list of persistent classes, etc), i. e. it won't make sense to change these properties without rebuilding the whole application, and you can leave it in its default location.

axtavt
  • 239,438
  • 41
  • 511
  • 482
2

You can achieve this by using spring-orm LocalContainerEntityManagerFactory (you don't need to use all the spring context stuff if you don't want to).

LocalContainerEntityManagerFactory lcemf = new LocalContainerEntityManagerFactory ();
lcemf.setPersistenceUnitName("some_pu");
lcemf.setpersistenceXmlLocation("file:/data/config.xml");
EntityManagerFactory emf = lcemf.createNativeEntityManagerFactory();

you can also ignore the xml once and for all, and configure all in code by using the LocalContainerEntityManagerFactory (see setPackagesToScan).

Frank Orellana
  • 1,820
  • 1
  • 22
  • 29
1

persistence.xml must always be present within META-INF directory in the classpath. More on where should META-INF be present for different types of java applications can be found here.

Persistence units are defined by the persistence.xml configuration file. The JAR file or directory whose META-INF directory contains persistence.xml is called the root of the persistence unit. The scope of the persistence unit is determined by the persistence unit’s root.

Each persistence unit must be identified with a name that is unique to the persistence unit’s scope.

Persistent units can be packaged as part of a WAR or EJB JAR file, or can be packaged as a JAR file that can then be included in an WAR or EAR file.

If you package the persistent unit as a set of classes in an EJB JAR file, persistence.xml should be put in the EJB JAR’s META-INF directory.

If you package the persistence unit as a set of classes in a WAR file, persistence.xml should be located in the WAR file’s WEB-INF/classes/META-INF directory.

Community
  • 1
  • 1
Vikdor
  • 23,934
  • 10
  • 61
  • 84
  • is the classpath for this the same as for loading packages? I tried putting a META-INF directory into the directory that our libs live in and it didn't work. Should that work if I didn't mess something else up? – BostonJohn Sep 20 '12 at 18:35
  • That would work as long as `META-INF` is in the top-level directory if you unpack the jar (using `jar xvf myapp.jar`). – Vikdor Sep 20 '12 at 18:36
  • But META-INF has to be in the jar then? (sorry, I phrase my earlier question poorly. I had tried putting it in the directory where I load external libraries from) – BostonJohn Sep 20 '12 at 18:49
  • Right, META-INF has to be in the jar, as a top-level directory. – Vikdor Sep 20 '12 at 18:50