zoukankan      html  css  js  c++  java
  • a different object with the same identifier value was already associated withthe session异常解决方案

    异常信息:

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

    发生原因:

    在对一个实体查询操作后,进行了set,然后又要对这个实体进行save/update,

    报错的例子

    //Service层
    public void modifyPersonalizedSettings(PersonalizedSettings ps,Integer userId)throws Exception {
    		
    		User user = personalizedSettingsDAO.selectUserById(userId);
    		ps.setUser(user);
    		personalizedSettingsDAO.updatePersonalizedSettings(ps);
    	}
    //DAO层
    public void updatePersonalizedSettings(PersonalizedSettings ps) throws Exception {
    System.out.println(ps+"DAO");
    template.merge(ps);
    }

    解决方案:

    最简单的就是不用save()方法,改用merge()方法。

    例子:

    //Service层
    public void modifyPersonalizedSettings(PersonalizedSettings ps,Integer userId)throws Exception {
    		
    		User user = personalizedSettingsDAO.selectUserById(userId);
    		ps.setUser(user);
    		personalizedSettingsDAO.updatePersonalizedSettings(ps);
    	}
    //DAO层
    public void updatePersonalizedSettings(PersonalizedSettings ps) throws Exception {
    System.out.println(ps+"DAO");
    //template.update(ps)不再使用update()方法,改用merge()方法即可
    template.merge(ps);
    }

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    python之set
    python之tuple
    python之list
    python之Number
    LAMP源码安装,搭建zabbix监控
    linux sshd服务
    linux rsync服务
    linux 实时同步inotify
    搭建LNMP;搭建WIKI
    数字,列表,函数
  • 原文地址:https://www.cnblogs.com/yunns/p/4867903.html
Copyright © 2011-2022 走看看