如何从persistence.xml引用外部hibernate.cfg.xml

时间:2013-10-23 20:51:39

标签: java persistence.xml hibernate.cfg.xml

我有一个需要部署到多个环境的JAR文件,每个环境都有自己的数据库连接参数。无论是对还是错,我都被要求在JAR外部建立这些数据库连接参数。我见过persistence.xml可以引用包含特定连接参数的外部hibernate.cfg.xml的示例。这是我的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>

外部hibernate.ejb.cfg与JAR文件位于同一文件夹中,但引用失败。这是我得到的错误:

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.

如何从persistence.xml访问外部hibernate.cfg.xml?

1 个答案:

答案 0 :(得分:1)

这里的错误是

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

正如评论中指出的那样,它在 WEB-INF / clases 文件夹中查找 将文件复制到该文件夹​​,并将以下行更改为:

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

干杯。