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

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

  • 相关阅读:
    redis redis-cli 操作指令
    Apache 配置默认编码
    Apache 查找httpd.conf文件
    Apache 错误日志
    dataTable 自定义排序
    bootstrap select2 参数详解
    获取元素滚动条高度
    TP5 操作DB is null is not null 条件
    TP5 自带分页类的传参
    jquery 获取 file 表单 上传的文件名
  • 原文地址:https://www.cnblogs.com/codingcloud/p/5262982.html
Copyright © 2011-2022 走看看