这两者的差别网上非常多资源,我这里就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. C# 线程知识汇总

    一.基本概念 进程(process)是windows系统中你的一个基本概念,它包含着一个运行程序所需要的资源.一个正在运行的应用程序在操作系统中被视为一个进程,进程可以包括一个或者多个线程.线程是操作 ...

  2. Memcached 与 Redis 的关键性能指标比较

    性能对比: Redis 只使用单核,而 Memcached 可以使用多核,所以平均每一个核上 Redis在存储小数据时比 Memcached 性 能更高. 而在 100k 以上的数据中,Memcach ...

  3. HDFS与java API应用

    java代码操作hadoop文件需要用hadoop的jar包,comment,hdfs,yarn,mapreduce,内均有有关jar包,eclipse操作hadoop还需要配置core-site.x ...

  4. SQL SERVER数据库状态

    一个SQL SERVER数据库会处于很多种状态,例如 ONLINE .RESTORING .RECOVERING .RECOVERY_PENDING  .SUSPECT.EMERGENCY .OFFL ...

  5. linux抓包命令-tcpdump命令详解

    最近调试支付接口的时候,遇到了一个奇怪的问题:我按照支付接口文档,对接支付通道,当消费业务正常后,调试查余和冲正的时候,支付通道的对接技术告诉我,系统没有我们支付系统的请求报文,数据库和日志中都没有, ...

  6. Linker scripts之SECTIONS

    1 Purpose The linker script describes how the sections in the input files should be mapped into the ...

  7. Windows2012R2 时间同步设置

    Windows2012R2里没有了internet时间,或者Internet时间无法同步成功,都可以尝试使用如下方法. 1.打开命令提示符, 输入:gpedit.msc,打开组策略管理器 2.执行上述 ...

  8. 使用Caffe预测遇到的问题

    1. 在使用网络预测图像时, prediction = net.predict( [input_image] ) 出现: net.image_dims[0] 不是整数情况, (2).甚至以为np.ze ...

  9. 4 Python+Selenium的元素定位方法(link/partial link)

    [环境] Python3.6+selenium3.0.2+IE11+win7 [定位方法] 1.link/partial link定位方法:定位的元素为文字链接且链接很长时 方法:find_eleme ...

  10. 洛谷P3111 [USACO14DEC]牛慢跑Cow Jog_Sliver 性质分析

    Code: #include<cstdio> #include<algorithm> #include<cstring> using namespace std; ...