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));
}

[转]解决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错误

    [转]解决a different object with the same identifier value was already associated with the session错误 这个错 ...

  2. hibernate中一种导致a different object with the same identifier value was already associated with the session错误方式及解决方法

    先将自己出现错误的全部代码都贴出来: hibernate.cfg.xml <?xml version="1.0" encoding="UTF-8"?> ...

  3. a different object with the same identifier value was already associated with the session:错误;

    当出现a different object with the same identifier value was already associated with thesession时,一般是因为在h ...

  4. org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session异常解决办法

    org.hibernate.NonUniqueObjectException: a different object with the same identifier value was alread ...

  5. a different object with the same identifier value was already associated with the session:

    hibernate操作: 实例化两个model类,更新时会提示  a different object with the same identifier value was already assoc ...

  6. Hibernate Error: a different object with the same identifier value was already associated with the session

    在执行Hibernate的Update操作时,报错:a different object with the same identifier value was already associated w ...

  7. a different object with the same identifier value was already associated with the session解决方案

    org.springframework.orm.hibernate3.HibernateSystemException: a different ]; nested exception ] at or ...

  8. Exception 06 : org.hibernate.NonUniqueObjectException: A different object with the same identifier value was already associated with the session :

    异常名称: org.hibernate.NonUniqueObjectException: A different object with the same identifier value was ...

  9. org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session:

    保存实体异常 https://blog.csdn.net/zzzz3621/article/details/9776539 org.hibernate.NonUniqueObjectException ...

随机推荐

  1. java批量insert入mysql数据库

    mysql 批量insert语句为 insert into Table_(col1,col2...) values(val11,val12...),(val11,val12...),...; java ...

  2. Linear Algebra lecture 2 note

    Lecture2 Elimination Inverses Permutation 消元法介绍(elimination): 有方程组 提取系数,形成矩阵为: 消元的思想跟解方程组中先消除未知数的思路一 ...

  3. 简述 C、C++程序编译的内存分配情况【转】

    面试题 9:简述 C.C++程序编译的内存分配情况  C.C++中内存分配方式可以分为三种:  (1)从静态存储区域分配:  内存在程序编译时就已经分配好,这块内存在程序的整个运行期间都存在.速度快. ...

  4. [课程设计]Scrum 1.5 多鱼点餐系统开发进度

    1.团队名称:重案组 2.团队目标:长期经营,积累客户充分准备,伺机而行 3.团队口号:矢志不渝,追求完美 4.团队选题:餐厅到店点餐系统WEB 5.Sprint 1时间:11.14-11.23 重案 ...

  5. 【转】Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing

    最近在看Java,在编译写书上一个例子时,由于书上的代码只有一部分,于是就自己补了一个内部类.结果编译时出现:No enclosing instance of type E is accessible ...

  6. Bash命令积累

    复制本目录下除掉所有的.png文件外的所有其他文件到某个目录下: mv -f !(*.png) ./src/ 除掉所有的.png文件外其余的文件,全部删掉 rm -f !(*.png) 本目录下,不显 ...

  7. stl::map之const函数访问

    如何在const成员数中访问stl::map呢?例如如下代码: string ConfigFileManager::MapQueryItem(const string& name) const ...

  8. Android开发中经常使用的Content-Type简介

    1.application/x-www-form-urlencoded:最常使用的类型(默认也是这种类型),主要用于提交不带文件的post数据. 2.multipart/form-data:需要结合b ...

  9. 调用C++动态链接库出现错误

    解决方式:将托管 System.String 中的内容复制到非托管内存(Marshal.StringToHGlobalAnsi) class HttpsSend { [DllImport(" ...

  10. Spring中javaMail通过SMTP发送邮件

    public final class Emails { /** The java mail sender. */ private static JavaMailSender javaMailSende ...