开始学习hibernate,是下载的最新版 4.3.8
按照以前的教程学习的时候,发现获取SessionFactory的方法已经过期了
于是我尝试按照官方的文档测试,
但是一直都是报异常,为此很是郁闷,在网上找了很多解决办法,但是大多数都没有用,
刚开始看到的错误信息是:could not read xml
一直以为是配置错误
最后有人说是获取SessionFactory的地方出错了
官方文档的代码如下:
try {
// Create the SessionFactory from hibernate.cfg.xml
new Configuration().configure().buildSessionFactory(
new StandardServiceRegistryBuilder().build() );
}
catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
改成如下:
try {
Configuration cfg = new Configuration().configure();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
.applySettings(cfg.getProperties()).buildServiceRegistry();
return cfg.buildSessionFactory(serviceRegistry);
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}