http://www.yanshare.com/hibernate-sessionfactory-creation/
昨天做项目开发的时候把Hibernate升级到了4.1.4版本,在创建SessionFactoty时意外发现以前用的buildSessionFactory()方法已经被deprecated,查看源码时发现有段这样的注解。
1
|
@deprecated Use
{@link #buildSessionFactory
(ServiceRegistry
)
} instead
|
于是发现,Hibernate从3.X到4.X的升级在创建SessionFactory上面有了大的修改,需要传一个Service做参数。下面给出4.X版本的创建SessionFactory方法。
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package
com.hibernate.util
;
import org.hibernate.cfg.Configuration ; import org.hibernate.service.ServiceRegistry ; import org.hibernate.service.ServiceRegistryBuilder ; import org.hibernate.HibernateException ; import org.hibernate.SessionFactory ; public class HibernateUtil { private static final SessionFactory sessionFactory = buildSessionFactory ( ) ; private static SessionFactory buildSessionFactory ( ) throws HibernateException { Configuration cfg = new Configuration ( ) ; cfg. configure ( ) ; ServiceRegistry serviceRegistry = new ServiceRegistryBuilder ( ). applySettings (cfg. getProperties ( ) ). buildServiceRegistry ( ) ; SessionFactory sf = cfg. buildSessionFactory (serviceRegistry ) ;; return sf ; } public static SessionFactory getSessionFactory ( ) { return sessionFactory ; } } |
另存顺便给出3.X的创建方法。
1
|
SessionFactory sf
=
new Configuration
(
).
configure
(
).
buildSessionFactory
(
)
;
|