hibernate获取session的两种方式的区别
来源:互联网 发布:移动网络ip地址是什么 编辑:程序博客网 时间:2024/05/22 13:34
hibernate获取session有两种方式,存在着一定的区别
获取session的三个步骤
// 1.创建Configuration,该对象用于读取hibernate.cfg.xml,并完成初始化
Configuration configuration = new Configuration().configure();
// 2.创建SessionFactory
SessionFactory sessionFactory = configuration.buildSessionFactory();
// 3.创建Session,相当于jdbc的Connection
Session session = sessionFactory.getCurrentSession();
或者Session session = sessionFactory.openSession();
区别如下:
①getCurrentSession()
这种方式获取到的session是与当前线程绑定的,是一个单例,且会自动关闭,如果再手动调用session.close()会报错。使用该单例session进行crud操作时必须开启事务,否则无法提交
getCurrentSession()内部实际上调用了openSession()
使用该方式还需要进行配置文件,在hibernate.cfg.xml中添加
<property name="current_session_context_class">thread</property>
②openSession()
这种方式获取的session需要手动关闭,在进行crud操作时,如果只是进行查询操作则无需开启事务,进行增删改时必须开启事务
- hibernate获取session的两种方式的区别
- hibernate获取session的两种方式
- hibernate获取session的两种方式
- Hibernate两种获取session的方式openSession和getCurrentSession的区别与理解
- hibernate两种获取session方法的区别
- hibernate 获得session的两种方式
- Request获取Session的两种方式
- hibernate中创建session的两种方式方式,区别在哪里?
- hibernate session的获取方式
- hibernate 获取Session 的方式
- hibernate获取session的两种方式分别是openSession() 方法和getCurrentSession() 方法。
- hibernate中取得session的两种方式
- hibernate中取得session的两种方式
- Struts2的获取Session的两种方式
- Hibernate中两种获取Session的方式
- Hibernate中两种获取Session的方式 (转)
- Hibernate中两种获取Session的方式
- Hibernate中两种获取Session的方式
- C/C++动态内存管理之calloc ,malloc,realloc的区别
- 20.日志配置
- java乐观锁的使用
- Mac安装MySQL5.7.19 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
- JXL 详解
- hibernate获取session的两种方式的区别
- BZOJ 4819: [Sdoi2017]新生舞会 01分数规划 二分图最大权匹配(KM算法)/费用流
- vue2.0下axios实现跨域踩的坑
- AndroidStudio 混淆打包
- hdu1879 继续畅通工程 最小生成树
- 用户的邮件激活
- C指针笔记
- Android绘制学习——基础形状
- Dubbox 编译、安装