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?

3
  • 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. Commented Feb 1, 2014 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. Commented Mar 20, 2014 at 3:41
  • Check this stackoverflow answer : stackoverflow.com/questions/23384413/… Commented Jul 26, 2016 at 20:03

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.