zoukankan      html  css  js  c++  java
  • [转]解决a different object with the same identifier value was already associated with the session错误

    1、a different object with the same identifier value was already associated with the session。

      错误原因:在hibernate中同一个session里面有了两个相同标识但是是不同实体。

      解决方法一:session.clean()

      PS:如果在clean操作后面又进行了saveOrUpdate(object)等改变数据状态的操作,有可能会报出"Found two representations of same collection"异常。

      解决方法二:session.refresh(object)

      PS:当object不是数据库中已有数据的对象的时候,不能使用session.refresh(object)因为该方法是从hibernate的session中去重新取object,如果session中没有这个对象,则会报错所以当你使用saveOrUpdate(object)之前还需要判断一下。

      解决方法三:session.merge(object)

      PS:Hibernate里面自带的方法,推荐使用。

    2、Found two representations of same collection

      错误原因:见1。

      解决方法:session.merge(object)

    以上两中异常经常出现在一对多映射和多对多映射中

     

    举一下自己的代码

     

        /**
         * 取得当前Session.
         * @return Session
         */
        public Session getSession() {
            return sessionFactory.getCurrentSession();
        }
    
        /**
         * 保存新增或修改的对象.
         * @param entity
         */
        public void save(final T entity) {
            getSession().saveOrUpdate(getSession().merge(entity));
        }

     

     

  • 相关阅读:
    在Linux终端命令行下播放音乐的命令
    危险,几条可致命的Linux命令!
    Linux 之 shell 比较运算符
    Linux-dd命令详解
    vi总结的几个技巧
    用iptables实现代理上网
    CentOS7.5 开启Samba服务
    CentOS7.5常用命令
    CentOS7.5 安装部署Apache+Mysql+Php
    C++入门基础笔记
  • 原文地址:https://www.cnblogs.com/linvan/p/6141052.html
Copyright © 2011-2022 走看看