gpt4 book ai didi

java - Hibernate可以读取hibernate.cfg.xml的内容但仍然给出错误?

转载 作者:行者123 更新时间:2023-12-01 13:38:33 25 4
gpt4 key购买 nike

我知道这是一个老问题,但它确实困住了我。

我接受使用classLoader获取inputsream的建议,代码如下:

 public static Session getSession() {
if (sessionFactory == null) {
InputStream xmlInputStream =
ResourceUtil.getInputStream(HibernateDBUtil.class, CFG_XML);
Scanner scanner = new Scanner(xmlInputStream);
//just a test I can read the xml file ,print content here
while(scanner.hasNext()) System.out.println(scanner.nextLine());
scanner.close();
//reget the stream
xmlInputStream =
ResourceUtil.getInputStream(HibernateDBUtil.class, CFG_XML);
Configuration cfg = new Configuration();
cfg.addInputStream(xmlInputStream).configure();
// version 4.3 's way to config
StandardServiceRegistryBuilder builder =
new StandardServiceRegistryBuilder().applySettings
(cfg.getProperties());
StandardServiceRegistryImpl registry =
(StandardServiceRegistryImpl) builder.build();

sessionFactory = cfg.buildSessionFactory(registry);
}
return sessionFactory.openSession();
}
private static SessionFactory sessionFactory;
private static final String CFG_XML = "properties/hibernate.cfg.xml";

我确认ResourceUtil.getInputStream工作正常,因为我可以打印hibernate.cfg.xml的内容,但为什么仍然给我错误:

更新:完整堆栈跟踪:

INFO: HHH000412: Hibernate Core {4.3.0.Final} Jan 10, 2014 11:33:43 PM org.hibernate.cfg.Environment

INFO: HHH000206: hibernate.properties not found Jan 10, 2014 11:33:43 PM org.hibernate.cfg.Environment buildBytecodeProvider

INFO: HHH000021: Bytecode provider name : javassist Jan 10, 2014 11:33:44 PM org.hibernate.cfg.Configuration configure INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml Jan 10, 2014 11:33:44 PM org.hibernate.cfg.Configuration getConfigurationInputStream

INFO: HHH000040: Configuration resource: /hibernate.cfg.xml Exception in thread "main" org.hibernate.HibernateException: /hibernate.cfg.xml not found at org.hibernate.internal.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:173)atorg.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:2093) at org.hibernate.cfg.Configuration.configure(Configuration.java:2074) at org.hibernate.cfg.Configuration.configure(Configuration.java:2054) at cn.edu.gdut.utils.HibernateDBUtil.getSession(HibernateDBUtil.java:49) at cn.edu.gdut.utils.HibernateDBUtil.listBasicElement(HibernateDBUtil.java:25) at cn.edu.gdut.utils.HibernateDBUtil.main(HibernateDBUtil.java:20)

最佳答案

我认为,文件在 IDE 中的位置并不重要。重要的是文件在执行期间在最终包中的位置。

所以,假设 ,您的 properties/文件夹应该成为执行包的根目录。这样,hibernate.cfg.xml 位于 properties/ 中,那么在执行期间它应该位于根目录中。您可以尝试使用

private static final String CFG_XML = "hibernate.cfg.xml";

已更新

Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");

下面继续您的代码。在hibernate.cfg.xml我提到<mapping resource="pkg/name/of/mapping/file/file.hbm.xml" />

关于java - Hibernate可以读取hibernate.cfg.xml的内容但仍然给出错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21045830/

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