不,这与this one或this one或this one不同。
原因很简单,我的* .hbm.xml文件已经在正确的目录中(src/main/resources/com/corp/dept/proj
)。
此外,它曾经运行正常,只要我静态(即在构建时硬编码)从hibernate.cfg.xml
获取所有内容,在我的HibernateUtil.java中使用这个简单的静态方法:
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
移到要在运行时读取的属性文件(例如DB的“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文件中的预期相同的子目录中。那么,为什么是这个错误?
建议的解决方法是什么?
答案 0 :(得分:1)
获取Configuration(sConfig)实例后,您尝试错误地添加XML映射文档。当您使用基于XML的映射文件(.hbm)时,要添加xml映射文件,您需要使用addResource()方法,而不是addClass()方法。
addClass()方法,用于添加基于注释的映射。
所以替换
sConfig.addClass(FooTbl.class);
与
sConfig.addResource("com/corp/dept/proj/FooTbl.hbm.xml");
希望这能解决您的问题。
答案 1 :(得分:0)
我找到了正确的解决方案:
不要使用静态初始化块路由,只需坚持使用原始的 static buildSessionFactory()方法并在两者之间注入Configuration.setProperty()
语句(根据需要) Configuration.configure()
和Configuration.BuildSessionFactory()
。也就是说,我没有在问题中发布的buildSessionFactory()的原始实现,而是:
private static SessionFactory buildSessionFactory() {
try {
Configuration config = new Configuration();
config = config.configure("resources/hibernate.cfg.xml");
config.setProperty("hibernate.connection.url", properties.getProperty("dburl"));
SessionFactory session = config.buildSessionFactory();
return session;
}
catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
无需延长Configuration
课程......工作就像一个魅力。 :)
答案 2 :(得分:0)
在addClass()方法的情况下,确保您的文件FooTbl.hbm.xml名称应与类名匹配。
这里你的类名是FooTbl,那么映射文件名应该是FooTbl.hbm.xml否则会生成异常。
试试这个。 :)