您的位置:首页 > 其它

hibernate 第一次学习遇到的问题

2015-01-19 10:21 281 查看
开始学习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);
}


不知道有没有更好的方法?
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: