不,這不是this one或this one或this one同樣的問題。org.hibernate.MappingNotFoundException:資源:COM/CORP /部門/凸出/ FooTbl.hbm.xml沒有找到
原因很簡單,我的* .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文件中的預期相同的子目錄中。所以,爲什麼是這個錯誤?
什麼是解決這個問題的建議方法?
嘗試一個測試並將FooTbl.hbm.xml複製到類文件夾,與您的.class文件位於相同 – fmodos
我注意到在您的日誌文件中,當它找到該文件時,它正在讀取' resources/com/corp/dept/proj/FooTbl.hbm.xml',當它沒有嘗試讀取'com/corp/dept/proj/FooTbl.hbm.xml'。 – azurefrog