4

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:

<?xml version="1.0" encoding="UTF-8"?>
<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">

    <persistence-unit name="myApp" transaction-type="RESOURCE_LOCAL">
        <properties>
            <property name="hibernate.ejb.cfgfile" value="./hibernate.cfg.xml" />
        </properties>
    </persistence-unit>
</persistence>

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:

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.

How can I access that external hibernate.cfg.xml from persistence.xml?

DT7
  • 1,615
  • 14
  • 26
user2913104
  • 41
  • 1
  • 4
  • I'm not sure but, have you tried without the dot before your slash. I think, if you write `value="/hibernate.cfg.xml"`, your server will find your configuration file into your lib/classes folder. – Patouche Feb 01 '14 at 19:13
  • It looks to me like it needs to be on your classpath, even if it is external to a jar file. Put it in a directory and add the directory to your classpath. Anytime you see `getResourceAsStream` you probably need to look at the classpath. – ngreen Mar 20 '14 at 03:41
  • Check this stackoverflow answer : http://stackoverflow.com/questions/23384413/how-to-externalize-properties-of-persistence-xml-for-jboss-7-1-1 – Guerino Rodella Jul 26 '16 at 20:03

1 Answers1

1

The error here is

Caused by: org.hibernate.HibernateException: C:/MyApp/lib/hibernate.cfg.xml not found

As it was pointed out in a comment, it looks for it in the folder WEB-INF/clases Copy the file to that folder and change the following line to:

<property name="hibernate.ejb.cfgfile" value="hibernate.cfg.xml" />

Cheers.

sdsc81
  • 570
  • 8
  • 18