gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 03:39:12 24 4
gpt4 key购买 nike

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

最佳答案

这里的错误是

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

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

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

干杯。

关于java - 如何从 persistence.xml 引用外部 hibernate.cfg.xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19552029/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com