HIbernate中openSession和getCurrentSession
这两者的差别网上非常多资源,我这里就copy一下了,然后有点问题的是今天遇到的问题。
openSession和getCurrentSession的根本差别在于有没有绑定当前线程,所以,用法有差异:
* openSession没有绑定当前线程,所以,使用完后必须关闭。
* currentSession和当前线程绑定,在事务结束后会自己主动关闭。
今天在复习Hibernate时,看到Hibernate检索方式的时候。写了一个小样例:
@Test
public void query01() {
SessionFactory sessionFactory = new Configuration().configure()
.buildSessionFactory();
Session session = sessionFactory.getCurrentSession();
/*
* 使用getCurrentSession()必须开启事物。否则抛出异常org.hibernate.HibernateException:
* createQuery is not valid without active transaction
*/
session.beginTransaction();
Query query = session.createQuery("from Employee");
System.out.println(query.list());
}
hibernate.cfg.xml中配置了
<property name="current_session_context_class">thread</property>
这里已经写了凝视。我遇到的问题就是这个,在进行查询的时候使用getCurrentSession居然抛出 createQuery is not valid without active transaction的异常,认为非常奇怪。
依照文档说:getCurrentSession()方法获取Session的机制应该是
“在getCurrentSession() 被调用的时候,实际被运行的方法是CurrentSessionContext.currentSession() 。在currentSession() 运行时。假设当前 Session为空。currentSession会调用 SessionFactory 的 openSession。
”
如今的状态是符合Session为空的情况,那么就应该通过openSession()方法产生一个Session。可是却抛出了异常。
Google了一下,找到一篇博文:http://liusu.iteye.com/blog/380397
里面介绍了关于这个问题,英文有点水,理解就看自己了。
我的感觉就是出现这样的情况感觉openSession相对来说还好用一些了。
@Test
public void query02() {
SessionFactory sessionFactory = new Configuration().configure()
.buildSessionFactory();
Session session = sessionFactory.openSession();;
try {
Query query = session.createQuery("from Employee");
System.out.println(query.list());
} catch (HibernateException e) {
e.printStackTrace();
}finally{
session.close();
}
}
可能比較片面。可是眼下还没有到那个层面,慢慢来,就像之前看这两个的差别一样。一直看不懂。慢慢的积累到一定层度就会非常好理解了。
HIbernate中openSession和getCurrentSession的更多相关文章
- hibernate中openSession()跟getCurrentSession()方法之间的区别
Hibernate openSession() 和 getCurrentSession的区别 getHiberanteTemplate .getCurrentSession和OpenSession 采 ...
- Hibernate中openSession() 与 getCurrentSession()的区别
1 getCurrentSession创建的session会和绑定到当前线程,而openSession每次创建新的session. 2 getCurrentSession创建的线程会在事务回滚或事物提 ...
- Hibernate之openSession与getCurrentSession的区别
openSession 与 getCurrentSession的区别(1)openSession 每一次获得的是一个全新的session对象,而getCurrentSession获得的是与当前线程绑定 ...
- Hibernate关于openSession和getCurrentSession的理解
来源(转载):http://blog.csdn.net/woshisap/article/details/7024482 1:getCurrentSession会把Session和当前的线程关联起来, ...
- hibernate中configuration和配置文件笔记
hibernate的核心类和接口 Configuration类 作用:(1)读取hibernate.cfg.xml文件 (2)管理对象关系映射文件<mapping resource=" ...
- 关于hibernate中的session与数据库连接关系以及getCurrentSession 与 openSession() 的区别
1.session与connection,是多对一关系,每个session都有一个与之对应的connection,一个connection不同时刻可以供多个session使用. 2.多个sessi ...
- hibernate 的SessionFactory的getCurrentSession 与 openSession() 的区别
1 getCurrentSession创建的session会和绑定到当前线程,而openSession不会. 2 getCurrentSession创建的线程会在事务回滚或事物提交后自动关闭,而ope ...
- Hibernate中的GetCurrentSession()方法
从3.0.1版本开 始,Hibernate增加了SessionFactory.getCurrentSession()方法. 采用getCurrentSession()创建的session在commit ...
- sessionFactory中的openSession和getCurrentSession的一些注意事项
今天进行Hibernate测试时遇到了一个问题 我在用sessionFactory生产seesion时出现了故障,使用getCurrentsesstion时产生异常: Exception in thr ...
随机推荐
- nyoj--239--月老的难题(最小点覆盖)
月老的难题 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 月老准备给n个女孩与n个男孩牵红线,成就一对对美好的姻缘. 现在,由于一些原因,部分男孩与女孩可能结成幸福的一 ...
- 【SDOI 2010】 计算器
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2242 [算法] 第一问用快速幂解决 第二问用exgcd解决 第三问用BSGS算法解决 ...
- 负载均衡获得真实源IP的6种方法
除了X-FORWARD-FOR,负载均衡中获得真实源IP的方法还有很多种. 本文抛砖引玉,主要介绍获得真实源IP的多种方法,而不是具体配置. 负载均衡获得真实IP的方法有很多种,将形成专题文章. 本文 ...
- expdp通过dblink远端导出
环境说明: db62是源端 rac数据库 dw03为需要导入的目标端数据库单机,实例名,服务名,字符串名都为dw03 数据库版本:11.2.0.4 操作系统:rehat 6.7 1.创建dblink ...
- H3BPM子表的复制
在做一个流程的时候,碰到了下面的表数据直接从上表中获取,并且为不可编辑状态,没有增加和删除行的按钮.一开始使用的是ComputationRule属性,但是有一项是日期空间,没有这个属性,不知道怎么处理 ...
- VirtualBox 5.1.14 获取VirtualBox COM对象错误
Oracle VM VirtualBox 5.1.14 安装完成之后显示错误, Failed to instantiate CLSID_VirtualBox w/ IVirtualBox, but C ...
- B - Guess a number!
Problem description A TV show called "Guess a number!" is gathering popularity. The whole ...
- Struts2的学习链接
---- Struts2的学习途径 (downpour) http://www.iteye.com/wiki/struts2/1306-struts2-way-of-learning ---- Str ...
- CSS浮动相关解决办法
浮动元素引起的问题: 1. 父元素的一级子元素全都为浮动元素的情况下,父元素的高度无法被撑开,影响与父元素同级的元素 2. 与浮动元素同级的非浮动元素(内容)会跟随其后 3. 若非第一个元素浮动,则该 ...
- 文档控件NTKO OFFICE 详细使用说明之预览Excel文件(查看、编辑、保存回服务器)
1.在线预览Excel文件 (1) 运行环境 ① 浏览器:支持IE7-IE11(平台版本还支持Chrome和Firefox) ② IE工具栏-Internet 选项:将www.ntko.com加入到浏 ...