title: How to reference external hibernate.cfg.xml from persistence.xml
body: <p>I have a JAR file that needs to be deployed into multiple environments, each with their own database connection parameters.  Rightly or wrongly, I've been requested to make those database connection parameters external to the JAR.  I've seen examples where persistence.xml can reference an external hibernate.cfg.xml that contains the specific connection parameters.  Here's my persistence.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence version="2.0"
      xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"&gt;
  
      &lt;persistence-unit name="myApp" transaction-type="RESOURCE_LOCAL"&gt;
          &lt;properties&gt;
              &lt;property name="hibernate.ejb.cfgfile" value="./hibernate.cfg.xml" /&gt;
          &lt;/properties&gt;
      &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p>That external hibernate.ejb.cfg is located in the same folder as the JAR file, but the reference is failing.  Here's the error I get:</p>
  
  <pre><code>DEBUG Ejb3Configuration - Look up for persistence unit: myApp
  DEBUG Ejb3Configuration - Detect class: true; detect hbm: true
  DEBUG Ejb3Configuration - Detect class: true; detect hbm: true
  DEBUG Ejb3Configuration - Creating Factory: myApp
  ERROR MyDaoImpl - initEntityManager() exception: 
  javax.persistence.PersistenceException: [PersistenceUnit: myApp] Unable to configure EntityManagerFactory
      at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:265)
      at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:125)
      at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:52)
      at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:34)
      at com.touchnet.MyApp.server.dao.MyDaoImpl.initEntityManager(MyDaoImpl.java:486)
      at com.touchnet.MyApp.server.dao.MyDaoImpl.getEntityManager(MyDaoImpl.java:457)
      at com.touchnet.MyApp.server.dao.MyDaoImpl.getTransaction(MyDaoImpl.java:512)
      at com.touchnet.MyApp.server.dao.MyDaoImpl.beginTransaction(MyDaoImpl.java:502)
      at com.touchnet.MyApp.server.service.MyAppService.loadInputFile(MyAppService.java:346)
      at com.touchnet.MyApp.server.commandLine.MyAppLoader.main(MyAppLoader.java:74)
      at com.touchnet.MyApp.server.commandLine.MyAppLauncher.main(MyAppLauncher.java:44)
  Caused by: org.hibernate.HibernateException: C:/MyApp/lib/hibernate.cfg.xml not found
      at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:147)
      at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:1411)
      at org.hibernate.cfg.Configuration.configure(Configuration.java:1433)
      at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:972)
      at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:753)
      at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:191)
      at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:253)
      ... 10 more
  ERROR MyDaoImpl - No EntityManager configured.
  DEBUG MyDaoImpl - getTransaction() returns null
  ERROR MyDaoImpl - No Transaction configured.
  </code></pre>
  
  <p>How can I access that external hibernate.cfg.xml from persistence.xml?</p>