Search code examples
jpaeclipselinkpersistence.xmlpersistence-unit

How to store persistence.xml with other configuration files


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.


Solution

  • 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.