Hibernate getCurrentSession()和openSession()的区别
- 通过getCurrentSession()创建的Session会绑定到当前线程上;openSession()不会。
- 通过getCurrentSession()获取Session,首先是从当前上下文中寻找旧的Session,如果没有,则创建新的Session;而openSession()是每次都创建新的Session。
- getCurrentSession()创建的Session在事物提交时自动关闭;openSession()创建的Session需要手动关闭。
@Test
public void testTeacherSave() { Teacher t = new Teacher(); t.setName("t1");
t.setTitle("middle");
t.setBirthDate(new Date()); Session session = sessionFactory.openSession();
//Session session = sessionFactory.getCurrentSession(); session.beginTransaction();
session.save(t); Session session2 = sessionFactory.getCurrentSession(); System.out.println(session == session2); session.getTransaction().commit(); Session session3 = sessionFactory.getCurrentSession(); System.out.println(session == session3); }输出:Hibernate:
insert
into
Teacher
(birthDate, gender, good, name, title)
values
(?, ?, ?, ?, ?)
false
false@Test
public void testTeacherSave() { Teacher t = new Teacher(); t.setName("t1");
t.setTitle("middle");
t.setBirthDate(new Date()); //Session session = sessionFactory.openSession();
Session session = sessionFactory.getCurrentSession(); session.beginTransaction();
session.save(t); Session session2 = sessionFactory.getCurrentSession(); System.out.println(session == session2); session.getTransaction().commit(); Session session3 = sessionFactory.getCurrentSession(); System.out.println(session == session3); }输出:Hibernate:
insert
into
Teacher
(birthDate, gender, good, name, title)
values
(?, ?, ?, ?, ?)
true
false
Hibernate getCurrentSession()和openSession()的区别的更多相关文章
- hibernate 的SessionFactory的getCurrentSession 与 openSession() 的区别
1 getCurrentSession创建的session会和绑定到当前线程,而openSession不会. 2 getCurrentSession创建的线程会在事务回滚或事物提交后自动关闭,而ope ...
- 关于hibernate中的session与数据库连接关系以及getCurrentSession 与 openSession() 的区别
1.session与connection,是多对一关系,每个session都有一个与之对应的connection,一个connection不同时刻可以供多个session使用. 2.多个sessi ...
- getCurrentSession 与 openSession() 的区别
1 getCurrentSession创建的session会和绑定到当前线程,而openSession不会. 2 getCurrentSession创建的线程会在事务回滚或事物提交后自动关闭,而ope ...
- hibernate中openSession()跟getCurrentSession()方法之间的区别
Hibernate openSession() 和 getCurrentSession的区别 getHiberanteTemplate .getCurrentSession和OpenSession 采 ...
- getCurrentSession 与 openSession区别
getCurrentSession () 使用当前的session openSession()重新建立一个新的session 使用SessionFactory.getCurrentSession()需 ...
- Hibernate与Mybatis的概念区别
首先简单介绍下两者的概念: Hibernate :Hibernate 是当前最流行的ORM框架,对数据库结构提供了较为完整的封装. Mybatis:Mybatis同样也是非常流行的ORM框架,主要着力 ...
- jdbc,mybatis,hibernate各自有优缺点以及区别
JDBC: 我们平时使用jdbc进行编程,大致需要下面几个步骤: 1,使用jdbc编程需要连接数据库,注册驱动和数据库信息 2,操作Connection,打开Statement对象 3,通过State ...
- Hibernate load 和 Get的区别
load和get都可以取回一个对象,难道是方法重复吗?绝对不可能,那它们到底有那些区别呢? 在http://blog.chinaunix.net/u/484/showart_1093166.html这 ...
- hibernate中get和load区别
在日常开发中,获取数据时必不可少的,这样就要用到get和load方法来实现了.下面简单说一下get和load的区别. 1.返回值不同 使用get方法检索数据时,没有该数据返回值为null. 而使用lo ...
随机推荐
- 修改placeholder提示内容的颜色以及文本框输入文字内容的颜色
一般表单文本框提示信息:placeholder=" ",默认颜色是灰色的,输入文本信息也是默认为黑色的,如图所示: 修改placeholder提示内容的颜色关键代码及实现: 实现输 ...
- BZOJ1778 [Usaco2010 Hol]Dotp 驱逐猪猡
首先我们列出转移矩阵$M$,$M_{i, j} = \frac {1 - \frac{p} {q}} {deg[i]}$(i,j之间有边)or $M_{i, j} = 0$(i,j之间没边) 则这个矩 ...
- 基础php链接SQL数据库
要连接到你的数据库必须添加以下几条数据: $conn = @mysql_connect("localhost","root","root") ...
- wpf中UserControl的几种绑定方式
我们经常会抽取一些可重用的控件,某个属性是否需要重用,直接决定了这个属性的绑定方式. 1.完全不可重用的控件 有一些与业务强相关的控件,它们的属性完全来自ViewModel,越是相对复杂的控件,越容易 ...
- MYSQL中关于日期处理的函数
< DOCTYPE HTML PUBLIC -WCDTD HTML TransitionalEN> MySQL数据库中SQL语句中 关于日期.时间\时间戳的函数 一 MySQL 获得当 ...
- 1、Android Bitmap详细介绍
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io ...
- Web页中table导出到execl(带模板)
1.将excel另存为html,将其复制到aspx文件中 2.输出格式为excel InitData(); Response.Clear(); Response.Buffer = true; Resp ...
- 网页设计、java、Andorid资源清单整理
多学多练做笔记很重要. 赤裸裸的干货非鸡汤 网页设计的主要技术: Html, Js, Css, Ps HTML/HTML5 网页的基础Html必须知道的.但 ...
- HDU 5358 尺取法+枚举
题意:给一个数列,按如下公式求和. 分析:场上做的时候,傻傻以为是线段树,也没想出题者为啥出log2,就是S(i,j) 的二进制表示的位数.只能说我做题依旧太死板,让求和就按规矩求和,多考虑一下就能发 ...
- bigworld源码分析(4)——BaseAppMgr分析
BaseAppMgr是用来管理BaseApp的,在整个bigworld中只有一个.本篇就BaseAppMgr的一些核心功能进行分析: (1) BaseAppMgr是如何通知BaseApp创建Entit ...