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 ( ) ;

Hibernate 中 SessionFactory的创建相关推荐

  1. hibernate中SessionFactory,Session的理解?

    Session接口         Session接口对于Hibernate   开发人员来说是一个最重要的接口.然而在Hibernate中,实例化的Session是一个轻量级的类,创建和销毁它都不会 ...

  2. Hibernate中SessionFactory配置

    SessionFactory配置 因为Hibernate被设计为可以在许多不同环境下工作,所以它有很多配置参数.幸运的是,大部分都已经有默认值了,Hibernate发行包中还附带有示例的hiberna ...

  3. hibernate之SessionFactory和Session

    1. 关于SessionFactory 1.) SessionFactory是一个与连接池的类差不多的东西,在这里存着多个Session-与数据库的会话(相当于connection) 2.) 因为连接 ...

  4. HIbernate中的事务

    什么是事务 在数据库操作中,一项事务(Transaction)是由一条或多条操作数据库的SQL语句组成的一个不可分割的工作单元.当事务中的所有操作都正常完成时,整个事务才能被提交到数据库中,如果有一项 ...

  5. Hibernate的执行流程——SessionFactory的创建

    Hibernate的执行流程: 1.创建Configuration类实例,用来读取并解析配置文件(如Hibernate.cfg.xml),一个Configuration实例代表hibernate所有P ...

  6. SessionFactory在Hibernate中的作用

    SessionFactory在Hibernate中实际上起到了一个缓冲区的作用 他缓冲了HIbernate自动生成SQL语句和其他的映射数据 还缓冲了一些将来有可能重复利用的数据 为了能创建一个Ses ...

  7. hibernate中session和sessionFactory的作用

    转载:https://blog.csdn.net/m0_37154839/article/details/79506466 https://www.cnblogs.com/baizhanshi/p/7 ...

  8. Hibernate中的sessionFactory是做什么用的?

    SessionFactory中取得Session的实例,SessionFactory在Hibernate中实际起到了一个缓冲区的作用,它缓冲了Hibernate自动生成的SQL语句和一些其它的映射数据 ...

  9. 在Spring、Hibernate中使用Ehcache缓存

    前一篇http://blog.csdn.net/ibm_hoojo/article/details/7739181介绍了Ehcache整合Spring缓存,使用页面.对象缓存:这里将介绍在Hibern ...

最新文章

  1. java.lang.UnsupportedOperationException: Required method destroyItem was not overridden
  2. js_sqlite_ADODB.Connection
  3. linux svn命令
  4. Golang 解决unsupported protocol scheme问题
  5. elasticsearch hbase
  6. Flink 读取文本文件,聚合每一行的uid
  7. linux删除目录tmpab是什么意思_linux 下tmp目录文件怎么被删除的?
  8. php target标签,为Typecho文章页url标签添加nofollow和target属性
  9. python培训价目表-参加python培训要多少钱?
  10. 算法:转换二叉查找树为最大值加上当前值的数Convert BST to Greater Tree
  11. python热力相关系数图_【Python可视化6】Seaborn之heatmap热力图
  12. 安全认证框架之Shiro详解
  13. linux支持hd610显卡吗,奔腾G4560核显怎么样且HD610相当于什么级别的显卡?
  14. libcef--在windows或者linux上创建一个简单的应用程序
  15. xsmax进入dfu模式_iPhone XR/XS/XS Max 何如进入恢复模式或 DFU 模式?
  16. 计算机配件价格上涨,显卡涨价风声再起 PC配件涨价什么时候是个头
  17. 2022年,北京将建设22.4公里的轨道交通
  18. c语言水仙花数作业,c语言水仙花数(c语言水仙花数的编程)
  19. java爬虫爬豆瓣图书_网络爬虫——爬取豆瓣图书
  20. JDK19都出来了~是时候梳理清楚JDK的各个版本的特性了【JDK17特性讲解】

热门文章

  1. 10.11 学习记录
  2. java架构师主要负责什么_Java架构师主要学什么 Java架构师工资多少
  3. DLL 注入的三种方法详解
  4. 29 架构师领导艺术
  5. oracle导入dmp文件
  6. java实现字符串反转
  7. GDAL 遥感 图像处理 锐化(Laplace算子、Sobel算子)
  8. mysql练习题记录之:学生表、课程表、成绩表、讲师表、等级表
  9. 电脑BIOS密码的设置与修改方法
  10. 软件工程课程周学习进度报告——第四周