gpt4 book ai didi

java - org.hibernate.MappingNotFoundException : resource: com/corp/dept/proj/FooTbl. hbm.xml 未找到

转载 作者:太空宇宙 更新时间:2023-11-04 06:55:16 26 4
gpt4 key购买 nike

不,这与 this one 不是同一个问题或this onethis one .

原因很简单,我的 *.hbm.xml 文件已经位于正确的目录中 (src/main/resources/com/corp/dept/proj)。

此外,只要我在 HibernateUtil.java 中使用这个简单的静态方法静态地(即在构建时硬编码)从 hibernate.cfg.xml 获取所有内容,它就可以正常运行:

private static SessionFactory buildSessionFactory() {
try {
return new Configuration().configure("resources/hibernate.cfg.xml").buildSessionFactory();
}
catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}

但是由于我想将一些参数从 hibernate.cfg.xml 移动到要在运行时读取的属性文件(例如数据库的“connection.url”):

static {
try {
sessionFactory = buildHibernateConfig().buildSessionFactory();
} catch (Throwable ex) {
System.out.println("Initial SessionFactory creation failed: " + ex.getMessage());
throw new ExceptionInInitializerError(ex);
}
}

private static Configuration buildHibernateConfig() throws FileNotFoundException, IOException {

Properties properties = new Properties();
sConfig = new Configuration().configure("resources/hibernate.cfg.xml");

try {
properties.load(ServiceUtils.class.getResourceAsStream(MYWS_PROPS));
}
catch (IOException e) {
LOG.severe("Cannot find properties file " + MYWS_PROPS);
throw new SecurityException("Server Error: couldn't open " + MYWS_PROPS + " file");
}

/*hardcoded*/sConfig.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");/*hardcoded*/
/*hardcoded*/sConfig.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");/*hardcoded*/
sConfig.setProperty("hibernate.connection.url", properties.getProperty("dburl"));

// Add Hibernate XML mappings
sConfig.addClass(FooTbl.class);

return sConfig;
}

我遇到运行时异常:

Apr 03, 2014 14:31:49 AM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final}
Apr 03, 2014 14:31:49 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.2.6.Final}
Apr 03, 2014 14:31:49 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Apr 03, 2014 14:31:49 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Apr 03, 2014 14:31:49 AM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: resources/hibernate.cfg.xml
Apr 03, 2014 14:31:49 AM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: resources/hibernate.cfg.xml
Apr 03, 2014 14:31:49 AM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: resources/com/corp/dept/proj/FooTbl.hbm.xml

Apr 03, 2014 14:31:49 AM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
Apr 03, 2014 14:31:49 AM org.hibernate.cfg.Configuration addClass
INFO: HHH000221: Reading mappings from resource: com/corp/dept/proj/FooTbl.hbm.xml
Apr 03, 2014 14:31:49 AM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: com/corp/dept/proj/FooTbl.hbm.xml
Initial SessionFactory creation failed: resource: com/corp/dept/proj/FooTbl.hbm.xml not found

Caused by: org.hibernate.MappingNotFoundException: resource: com/corp/dept/proj/FooTbl.hbm.xml not found
at org.hibernate.cfg.Configuration.addResource(Configuration.java:712)
at org.hibernate.cfg.Configuration.addClass(Configuration.java:757)
at com.corp.dept.proj.myws.HibernateUtil.buildHibernateConfig(HibernateUtil.java:141)
at com.corp.dept.proj.myws.HibernateUtil.<clinit>(HibernateUtil.java:56)

现在...在出现异常之前,日志显示已找到文件。我还验证了这些文件实际上包含在 WAR 文件中,位于与预期相同的子目录中。那么,为什么会出现此错误?

解决这个问题的推荐方法是什么?

最佳答案

获取配置 (sConfig) 实例后,您尝试错误地添加 XML 映射文档。由于您使用的是基于 XML 的映射文件 (.hbm),因此要添加 xml 映射文件,您需要使用 addResource() 方法,而不是 addClass() 方法

addClass() 方法用于添加基于注释的映射。

所以替换

sConfig.addClass(FooTbl.class);

sConfig.addResource("com/corp/dept/proj/FooTbl.hbm.xml");

希望这能解决您的问题。

关于java - org.hibernate.MappingNotFoundException : resource: com/corp/dept/proj/FooTbl. hbm.xml 未找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22846410/

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