这两者的差别网上非常多资源,我这里就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的更多相关文章

  1. hibernate中openSession()跟getCurrentSession()方法之间的区别

    Hibernate openSession() 和 getCurrentSession的区别 getHiberanteTemplate .getCurrentSession和OpenSession 采 ...

  2. Hibernate中openSession() 与 getCurrentSession()的区别

    1 getCurrentSession创建的session会和绑定到当前线程,而openSession每次创建新的session. 2 getCurrentSession创建的线程会在事务回滚或事物提 ...

  3. Hibernate之openSession与getCurrentSession的区别

    openSession 与 getCurrentSession的区别(1)openSession 每一次获得的是一个全新的session对象,而getCurrentSession获得的是与当前线程绑定 ...

  4. Hibernate关于openSession和getCurrentSession的理解

    来源(转载):http://blog.csdn.net/woshisap/article/details/7024482 1:getCurrentSession会把Session和当前的线程关联起来, ...

  5. hibernate中configuration和配置文件笔记

    hibernate的核心类和接口 Configuration类 作用:(1)读取hibernate.cfg.xml文件 (2)管理对象关系映射文件<mapping resource=" ...

  6. 关于hibernate中的session与数据库连接关系以及getCurrentSession 与 openSession() 的区别

    1.session与connection,是多对一关系,每个session都有一个与之对应的connection,一个connection不同时刻可以供多个session使用.   2.多个sessi ...

  7. hibernate 的SessionFactory的getCurrentSession 与 openSession() 的区别

    1 getCurrentSession创建的session会和绑定到当前线程,而openSession不会. 2 getCurrentSession创建的线程会在事务回滚或事物提交后自动关闭,而ope ...

  8. Hibernate中的GetCurrentSession()方法

    从3.0.1版本开 始,Hibernate增加了SessionFactory.getCurrentSession()方法. 采用getCurrentSession()创建的session在commit ...

  9. sessionFactory中的openSession和getCurrentSession的一些注意事项

    今天进行Hibernate测试时遇到了一个问题 我在用sessionFactory生产seesion时出现了故障,使用getCurrentsesstion时产生异常: Exception in thr ...

随机推荐

  1. 【转】git rebase简介(基本篇)

    原文网址:http://blog.csdn.net/hudashi/article/details/7664631/ 原文: http://gitbook.liuhui998.com/4_2.html ...

  2. Codeforces Round #512 (Div. 2) D.Vasya and Triangle 数学

    题面 题意:给你n,m,k,在你在(0,0)到(n,m)的矩形内,选3个格点(x,y都是整数),使得三角形面积为n*m/k,不能找到则输出-1 题解:由毕克定理知道,格点多边形的面积必为1/2的整数倍 ...

  3. quartz + spring 启动项目时,报错The web application [] appears to have started a thread named.........

    只是想记录自己的错误信息,下次再出现就知道怎么操作,不用再查找资料 解决办法: package com.wqq.quartz_test.schedule; import javax.servlet.S ...

  4. RMS:均方根值,RMSE:均方根误差,MSE:标准差

    .均方根值(RMS),有时也称方均根.效值.英语写为:Root Mean Square(RMS). 美国传统词典的定义为:The square root of the average of squar ...

  5. Css 分类 属性 选择器

    Css 层叠样式表 美化页面的小工具 分类: 内联 (行内)在标签内部以属性的形式呈现,属性名style 内嵌 head标签内以标签形式呈现,标签名style 外部 head标签内 加link标签 引 ...

  6. vue路由history模式下打包node服务器配置

    vue-router 默认 hash 模式 —— 使用 URL 的 hash 来模拟一个完整的 URL,于是当 URL 改变时,页面不会重新加载. 如果不想要很丑的 hash,我们可以用路由的 his ...

  7. 基于Intent实现Activity与Activity之间的数据传递,实现二个Activity的跳转功能

    在讲参数传递之前,先讲下intent的定义: Intent intent = new Intent(MainActivity.this,SecondActivity.class);  //这是显式定义 ...

  8. GEF中连接的实现

    在GEF绘图笔想象中复杂许多,需要很多组件的依赖与支持,稍微弄错一个引用,或一个操作调试起来就比较麻烦,下面列一下实现一个连接线功能所需要实现的类及添加的方法 建议大图查看. 相关代码:参考<G ...

  9. [转]C++内存管理

    [导语] 内存管理是C++最令人切齿痛恨的问题,也是C++最有争议的问题,C++高手从中获得了更好的性能,更大的自由,C++菜鸟的收获则是一遍一遍的检查代码和对C++的痛恨,但内存管理在C++中无处不 ...

  10. 计算机二级考试Access教程

    本教程对编程语言各种要点进行详细的讲解介绍,从基础知识到实用技术功能,内容涵盖了从数组,类等基本概念到多态.模板等高级概念.教程本着实用的原则,每一小节都结合了可以笔试.面试的常见程序实例,以便从第一 ...