Illegal attempt to associate a collection with two open session

本文转载自:

http://whq0417.blog.163.com/blog/static/12795144120098150238663/?NI5TH7NZFE=yiFfuCpBty

 

Hibernate交给spring 管理了...

我的出现上述问题了..代码如下:

/** 修改 **/

    public void update(Object entity) {

        super.getHibernateTemplate().update(entity);

    }

    /** 根据Id查找* */

    public Restype queryById(Object entityid) {

        String sql = "from Restype where typeid = " + entityid;

        Restype restype = null;

        Query query = super.getSession().createQuery(sql);

        List type = query.list();

        if (type.size() > 0) {

            restype = (Restype) type.get(0);

        }

        return restype;

    }

只查询或者只修改都没有问题,问题是2者一起用,就报上面的错..原因就是开了2个seesion:

搞了2天终于解决了..

将查找方法修改为:

    /** 根据Id查找* */

    public Restype queryById(Object entityid) {

        String sql = "from Restype where typeid = " + entityid;

        Restype restype = null;

        List list = super.getHibernateTemplate().find(sql);

        if (list.size() > 0) {

            restype = (Restype) list.get(0);

        }

        return restype;

    }

就好了..

大家可以参考:http://thiger.blog.hexun.com/22094206_d.html

 

后来又遇到了这个问题:

就把更新方法中的update 换成了 merge 解决了问题

大家可以参考:http://www.jiehoo.com/hibernate%E5%92%8Cspring%E9%85%8D%E5%90%88%E4%BD%BF%E7%94%A8%E7%9A%84%E4%B8%80%E4%B8%AA%E9%97%AE%E9%A2%98.htm

或者:http://xiaandong.spaces.live.com/blog/cns!C7C9E9B51C60807!162.entry

 

merge(Object entity)

           Copy the state of the given object onto the persistent object with the same identifier.

update(Object entity)

           Update the given persistent instance, associating it with the current Hibernate Session.

参考文档:如果你确定当前session没有包含与之具有相同持久化标识的持久实例,使用update()。 如果想随时合并你的的改动而不考虑session的状态,使用merge()

我的理解:update只能在保证在session中只有一个持久化标示时,可以使用update,如果你在从数据库或者其他地方得到了另一个持久化对象,标示跟以前的某个对象一样,这个时候,只能用merge

错误情况:使用update有时候会出现Illegal attempt to associate a collection with two open sessions的异常,但是在取得对象后使用session.close()也不行,会出现另外的一个异常session is not open or is closed,原来就是getHibernateTemplate().update(object),修改为getHibernateTemplate().merge(object)问题解决。

merge

Copy the state of the given object onto the persistent object with the same identifier. If there is no persistent instance currently associated with the session, it will be loaded. Return the persistent instance. If the given instance is unsaved, save a copy of and return it as a newly persistent instance. The given instance does not become associated with the session. This operation cascades to associated instances if the association is mapped with cascade="merge".

The semantics of this method are defined by JSR-220.

Parameters:

object - a detached instance with state to be copied

Returns:

an updated persistent instance

Throws:

HibernateException

 

  有几种情况:

  1.如果idertifier(以下简称为id)为空或不存在,则进行inert动作

  2.如果id存在,则进行update动作

  至于会话层的就如文档上所说,不再多说。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值