我正在使用 Hibernate 进入 Eclipse RCP 应用程序。我为休眠映射创建了一个“服务”插件 - daos 等。为了测试,我在这个插件中创建了一个“主”类/方法。“Main”执行HibernateUtil.initSessionFactory();
,然后对数据库执行一些关于所有映射的查询。它工作正常。当我从另一个 Eclipse 插件(例如“ui”)执行相同的方法HibernateUtil.initSessionFactory();
时,我收到一个:
java.lang.ExceptionInInitializerError:org.hibernate.MappingNotFoundException:资源:de/da/service/data/TblItem.hbm.xml 未找到
包含映射时的异常:
addResource("de/da/service/data/TblItem.hbm.xml")
为什么会发生这种情况,而同一方法的本地调用工作正常?
public static void initSessionFactory() {
try {
SessionFactory sf = getSessionFactory(sessionFactory);
Session s = sf.getCurrentSession();
s.beginTransaction();
s.getTransaction().commit();
}
catch (Throwable ex) {
if (ConnectionException.analyze(ex)) {
throw new ConnectionException();
}
}
}
public static SessionFactory getSessionFactory() {
SessionFactory current = null;
if (config == null)
config = new AnnotationConfiguration().configure();
current = getSessionFactory(sessionFactory);
return current ;
}
private static SessionFactory getSessionFactory(SessionFactory sFactory) {
if (sFactory == null) {
try {
config = new AnnotationConfiguration().
setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLInnoDBDialect").
setProperty("hibernate.current_session_context_class", "thread").
setProperty("hibernate.connection.driver_class", "org.gjt.mm.mysql.Driver").
setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/db").
setProperty("hibernate.connection.username", "username").
setProperty("hibernate.connection.password", "password").
setProperty("hibernate.connection.pool_size", "0").
setProperty("hibernate.connection.autocommit", "true").
setProperty("hibernate.show_sql", "true").
setProperty("hibernate.format_sql", "true").
setProperty("hibernate.jdbc.batch_size", "0").
addResource("de/da/service/data/TblItem.hbm.xml").
sFactory = config.buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex.toString());
}
}
return sFactory;
}