4

我有一个 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?

4

1 回答 1

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" />

干杯。

于 2017-09-27T14:52:11.543 回答