【问题标题】:How to reference external hibernate.cfg.xml from persistence.xml如何从 persistence.xml 引用外部 hibernate.cfg.xml
【发布时间】:2013-10-23 20:51:39
【问题描述】:

我有一个需要部署到多个环境中的 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?

【问题讨论】:

  • 我不确定,但是,您是否尝试过在斜线前不加点。我想,如果你写value="/hibernate.cfg.xml",你的服务器会在你的 lib/classes 文件夹中找到你的配置文件。
  • 在我看来它需要在你的类路径中,即使它在 jar 文件的外部。将它放在一个目录中并将该目录添加到您的类路径中。每当您看到 getResourceAsStream 时,您可能需要查看类路径。
  • 检查这个 stackoverflow 答案:stackoverflow.com/questions/23384413/…

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


【解决方案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" />

干杯。

【讨论】:

    猜你喜欢
    • 2018-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-27
    • 2019-05-07
    • 1970-01-01
    • 2011-05-31
    • 1970-01-01
    相关资源
    最近更新 更多