今天写hibernate时候遇到一些异常

代码:

Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
session.save(t);
session.getTransaction().commit();
session.close();
System.out.println(t.getId());

出现异常情况:

出现以上原因是Session关闭

如果不是使用的SessionFactory.getSession()来获得Session。

        而是使用SessionFactory.getCurrentSession()方法来获得Session时,当事务结束的时候,不管是提交还是回滚事务,hibernate会自动关闭Session的,

所以不需要手动关闭。

hibernate的异常 Session was already closed的更多相关文章

  1. org.hibernate.LazyInitializationException...no session or session was closed

    org.hibernate.LazyInitializationException:failed to lazily initialize a collection of role:cn.its.oa ...

  2. hibernate报错org.hibernate.SessionException: Session was already closed

    org.hibernate.SessionException: Session was already closedat org.hibernate.internal.SessionImpl.clos ...

  3. SSH dao层异常 org.hibernate.HibernateException: No Session found for current thread

    解决方法: 在 接口方法中添加 事务注解 即可. public interface IBase<PK extends Serializable, T> { @Transactional v ...

  4. 【转】Hibernate 常见异常

    转载地址:http://smartan.iteye.com/blog/1542137 Hibernate 常见异常net.sf.hibernate.MappingException        当出 ...

  5. Hibernate 常见异常

    Hibernate 常见异常net.sf.hibernate.MappingException        当出现net.sf.hibernate.MappingException: Error r ...

  6. org.hibernate.HibernateException: No Session found for current thread

    spring.springmvc和hibernate整合 在sessionFactory.getCurrentSession()时,出现以下异常 No Session found for curren ...

  7. 分析 org.hibernate.HibernateException: No Session found for current thread

    /**      *      * org.hibernate.HibernateException: No Session found for current thread      * 分析:ge ...

  8. 关于 org.hibernate.TransientObjectException 异常

    在Hibernate的关联关系中,如果一个对象A 持有 对象 B 的引用,如果先保存 A ,再保存B,那么会出现 org.hibernate.TransientObjectException 异常. ...

  9. (八) Hibernate中的Session以及事务

    HibernateUtil.getSessionFactory().getCurrentSession() 和HibernateUtil.getSession() 的区别: 1.异:getCurren ...

随机推荐

  1. js 各种循环遍历

    js 各种循环遍历(表格比较) 遍历方法 能否遍历数组 能否遍历对象 备注 for 能 不能 for in 能(有诸多缺点) 能 为遍历对象而设计的,不适用于遍历数组 forEach 能 不能 bre ...

  2. yum运行时提示被锁住了解决办法

    1.当大家用linux的yum时,是不是经常会遇到下面的情况Loaded plugins: fastestmirrorExisting lock /var/run/yum.pid: another c ...

  3. ERROR 1130 (HY000): Host '192.168.20.165' is not allowed to connect to this MySQL server

    问题 远程连接mysql时遇到如下问题: ERROR 1130 (HY000): Host '192.168.20.165' is not allowed to connect to this MyS ...

  4. 使用redis防止商品超发

    redis不仅仅是单纯的缓存,它还有一些特殊的功能,在一些特殊场景上很好用.redis中key的原子自增incrby和判断key不存在再写入的setnx方法,可以有效的防止超发. 下面使用两个不同的方 ...

  5. dede 添加自定义函数

    include/extend.func.php里添加函数 function GetTopTags($str){$dsql = new Dedesql(false);$row = $dsql->G ...

  6. 阿里云内网和公网NTP服务器和其他互联网基础服务时间同步服务器

    阿里云为云服务器ECS提供了内网NTP服务器,对于阿里云以外的设备,阿里云同时提供了 公网NTP服务器,供互联网上的设备使用. 内网和公网NTP服务器 以下为阿里云提供的内网和公网NTP服务器列表. ...

  7. div+Css绝对定位(absolute)和相对定位(relative)的总结

    1.没有外Div的情况下 设置绝对定位(absolute)和相对定位(relative)是没有区别的 2.相对定位占位置 而绝对定位不占位置 会漂浮在别的Div之上 3.若父Div没有设置定位,子Di ...

  8. javascript面向对象之Object.defineProperty(a,b,c)

    /* Object.defineProperty(a,b,c);介绍 a:需要属性设置的对象 b:需要设置的属性名,(键值) c:是一个用于描述属性值得json数据.这个json数据有configur ...

  9. 第10课 C++异常简介

    1. try-catch语句 (1)try语句处理正常代码逻辑 (2)catch语句处理异常情况 (3)try语句中的异常由对应的catch语句处理 (4)C++通过throw语句抛出异常信息 2. ...

  10. 基于Linux的Samba开源共享解决方案测试(六)

    在极限读场景下,对于客户端的网络监控如图: 在极限写场景下,对于NAS1网关的网络监控如图: 在极限写场景下,对于NAS2网关的网络监控如图: 在极限写场景下,对于客户端的网络监控如图: 在极限混合读 ...