关于Hibernate4.0之后如何创建SessionFactory

在Hibernate中SessionFactory的重要性不用多说,但是不同版本的Hibernate创建SessionFactory却不同,在4.0之后,它舍弃了以往的创建方式:

 Configuration configuration = new Configuration();
 SessionFactory sf = configuration.buildSessionFactory();
1:Hibeinate 4.0大家可以尝试运用这种方式创建SessionFactory:

//实例化Configuration 通过该对象的configure()方法加载hibernate.cfg.xml
		Configuration conf=new Configuration().configure();
//创建SessionFactory
		StandardServiceRegistryBuilder serviceRegistryBuilder=new StandardServiceRegistryBuilder();
		ServiceRegistry serviceRegistry=serviceRegistryBuilder.build();
		SessionFactory sf=conf.buildSessionFactory(serviceRegistry);

serviceRegistry是Hibernate4.0引入的新机制,如果想要了解serviceRegistry大家可以去看看:

Hibernate ORM 新特性之 Service(Registry):http://planet.jboss.org/post/hibernate_orm_service_registry


2:Hibernate 4.1 又舍弃了StandardServiceRegistryBuilder,可以通过如下方式创建:

//Hibernate4.1创建SessionFactory
		ServiceRegistryBuilder serviceRegistryBuilder=new ServiceRegistryBuilder().applySettings(conf.getProperties());
		ServiceRegistry registry=serviceRegistryBuilder.buildServiceRegistry();
		SessionFactory sf=conf.buildSessionFactory(registry);
其他疑问大家可以去官方文档查找:

在你们下载的Hibernate包中:documentation\manual\en-US\html\ch03.html



你可能感兴趣的:(Hibernate)