1. 通过getCurrentSession()创建的Session会绑定到当前线程上;openSession()不会。
  2. 通过getCurrentSession()获取Session,首先是从当前上下文中寻找旧的Session,如果没有,则创建新的Session;而openSession()是每次都创建新的Session。
  3. 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()的区别的更多相关文章

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

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

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

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

  3. getCurrentSession 与 openSession() 的区别

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

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

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

  5. getCurrentSession 与 openSession区别

    getCurrentSession () 使用当前的session openSession()重新建立一个新的session 使用SessionFactory.getCurrentSession()需 ...

  6. Hibernate与Mybatis的概念区别

    首先简单介绍下两者的概念: Hibernate :Hibernate 是当前最流行的ORM框架,对数据库结构提供了较为完整的封装. Mybatis:Mybatis同样也是非常流行的ORM框架,主要着力 ...

  7. jdbc,mybatis,hibernate各自有优缺点以及区别

    JDBC: 我们平时使用jdbc进行编程,大致需要下面几个步骤: 1,使用jdbc编程需要连接数据库,注册驱动和数据库信息 2,操作Connection,打开Statement对象 3,通过State ...

  8. Hibernate load 和 Get的区别

    load和get都可以取回一个对象,难道是方法重复吗?绝对不可能,那它们到底有那些区别呢? 在http://blog.chinaunix.net/u/484/showart_1093166.html这 ...

  9. hibernate中get和load区别

    在日常开发中,获取数据时必不可少的,这样就要用到get和load方法来实现了.下面简单说一下get和load的区别. 1.返回值不同 使用get方法检索数据时,没有该数据返回值为null. 而使用lo ...

随机推荐

  1. junit高级篇(参数化、打包测试)-实例代码

    工程目录: 参数化测试,SquareTest.java: import static org.junit.Assert.*; import java.util.Arrays; import java. ...

  2. Spring知识点

    IOC: Inversion of Control 控制反转 ①自己控制→容器控制 ②控制具体实现→控制抽象(接口) DI:Dependency Injection 依赖注入 依赖于容器注入的对象 注 ...

  3. Linux有用命令

    1.nohup命令 使用方法:nohup ./run.sh 作用:忽略所有挂断(SIGHUP)信号,不挂断地运行命令

  4. Linux字符设备

    一.linux系统将设备分为3类:字符设备.块设备.网络设备. 字符设备:是指只能一个字节一个字节读写的设备,不能随机读取设备内存中的某一数据,读取数据需要按照先后数据.字符设备是面向流的设备,常见的 ...

  5. [ie兼容]ie7浮动左在前,浮动右在后导致右边浮动的元素掉下来

    解决办法:左浮动和右浮动元素在结构上互换位置 http://blog.sina.com.cn/s/blog_818a1e5b0100wp5b.html

  6. PHP 数组

    // 这里用数字来作为索引 $myArray = array(2012, 'blue', 5, 'BMW'); // 这个用关键字作为索引 $myAssocArray = array('year' = ...

  7. priority_queue 优先队列用法

    //采用默认优先关系: //(priority_queue<int>que;) //Queue 0: // 91 83 72 56 47 36 22 14 10 7 3 // //采用结构 ...

  8. JQuery源码解析(十一)

    内存泄露 什么是内存泄露? 内存泄露是指一块被分配的内存既不能使用,又不能回收,直到浏览器进程结束.在C++中,因为是手动管理内存,内存泄露是经常出现的事情.而现在流行的C#和Java等语言采用了自动 ...

  9. PCL中point cloud的数据类型

    出处: http://wiki.ros.org/pcl/Overview 1.数据类型 1.1 ROS中point cloud数据类型 sensor mesgs::PointCloud sensor ...

  10. iscroll动态加载数据完美解决方案

    @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta charset="utf-8&quo ...