这段时间刚开始学习Hibernate,到官网下文件包,发现刚出来的hibernate-release-4.0.0.CR6,于是下了下来,对着书上的例子一敲,问题来了...见下图
没办法,又重新进官网下了个hibernate-distribution-3.6.8.Final,发现没有问题。
于是乎......在官网和4.0源文件中穿插研究了几个小时,终于搞明白怎么回事了。
Hibernate4.0新增了一个ServiceRegistry接口,应用于Hibernate的配置或者服务等将统一向这个ServiceRegistry注册后才能生效。所以需要构建一个ServiceRegistry对象,将配置信息向它注册,然后Configuration对象根据从这个ServiceRegistry对象中获取配置信息生成SessionFactory。
那么这个ServiceRegistry怎么来呢?看看源代码结构就能发现它的来源地了——org.hibernate.service.ServiceRegistryBuilder。
分析一下这个ServiceRegistryBuilder的源文件,得出以下新的构建SessionFactory的方式:
-
Configuration config = new Configuration();
-
SessionFactory sf = config.buildSessionFactory(
-
new ServiceRegistryBuilder()
-
.configure()
-
.buildServiceRegistry());
Configuration config = new Configuration();
SessionFactory sf = config.buildSessionFactory(
new ServiceRegistryBuilder()
.configure()
.buildServiceRegistry());
却发现仍然有问题,居然是读取hibernate.cfg.xml文件出错,异常如下:
[org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'hibernate-configuration'.]
该文件经hibernate3测试通过,不可能出错,难道是要用hibernate-configuration-4.0.dtd?打开浏览器输入地址,发现没有这个文件,无奈,不用xml文件配置,使用hibernate.properties进行配置,修改代码如下:
-
Configuration config = new Configuration();
-
SessionFactory sf = config.buildSessionFactory(
-
new ServiceRegistryBuilder()
-
.buildServiceRegistry());
Configuration config = new Configuration();
SessionFactory sf = config.buildSessionFactory(
new ServiceRegistryBuilder()
.buildServiceRegistry());
运行测试通过!
不过,这只是很肤浅的用法,更深入的用法尚在摸索中...
简单得出总结,Hibernate4的配置入口不再是Configuration对象,而是ServiceRegistry对象,Configuration对象将通过ServiceRegistry对象获取配置信息(官方说法是为了方便统一管理)。但是使用XML文件进行配置仍有不知名的问题,盼早日明晓。
以上见解仅仅是自己的一份学习笔记,希望能帮助到一些有同样疑惑的朋友,本人也只是刚开始学习Hibernate,其中肯定有不成熟与草率之处,希望各位看到的朋友多多交流指正,谢谢!
补充:
更正规与专业的用法可参照Hibernate 4.0源代码中Configuration#buildSessionFactory()的实现:
-
public SessionFactory buildSessionFactory() throws HibernateException {
-
Environment.verifyProperties( properties );
-
ConfigurationHelper.resolvePlaceHolders( properties );
-
final ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
-
.applySettings( properties )
-
.buildServiceRegistry();
-
setSessionFactoryObserver(
-
new SessionFactoryObserver() {
-
@Override
-
public void sessionFactoryCreated(SessionFactory factory) {
-
}
-
-
@Override
-
public void sessionFactoryClosed(SessionFactory factory) {
-
( (StandardServiceRegistryImpl) serviceRegistry ).destroy();
-
}
-
}
-
);
-
return buildSessionFactory( serviceRegistry );
-
}
public SessionFactory buildSessionFactory() throws HibernateException {
Environment.verifyProperties( properties );
ConfigurationHelper.resolvePlaceHolders( properties );
final ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
.applySettings( properties )
.buildServiceRegistry();
setSessionFactoryObserver(
new SessionFactoryObserver() {
@Override
public void sessionFactoryCreated(SessionFactory factory) {
}
@Override
public void sessionFactoryClosed(SessionFactory factory) {
( (StandardServiceRegistryImpl) serviceRegistry ).destroy();
}
}
);
return buildSessionFactory( serviceRegistry );
}
分享到:
阅读(11256) | 评论(0) | 转发(0) |
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;
private static SessionFactory configureSessionFactory() throws HibernateException {
Configuration configuration = new Configuration();
configuration.configure();
serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
return sessionFactory;
}
不错,谢谢
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;
private static SessionFactory configureSessionFactory() throws HibernateException {
Configuration configuration = new Configuration();
configuration.configure();
serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
return sessionFactory;
}
*注:本文引自http://miniknife.iteye.com/blog/1260693