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

来源:互联网 发布:网络兼职小说打字 编辑:程序博客网 时间:2024/06/05 15:44
a different object with the same identifier value was already associated with the session:以及
.org.springframework.jdbc.UncategorizedSQLException: Hibernate operation: Could not execute JDBC batch update;
原因: 在一个session中存在2个对象但这2个对象的标志位是相同的比如2个对象的 id值是一样的 而这又是数据库的唯一主键
在做更新操作的时候就出现冲突了,因为Hibernate不知道到底要去更新哪个对象
网上解决办法:
hibernate2.17中使用insertOrUpdate()方法
hibernate3.0以上使用merge()来合并两个session中的同一对象
出现的环境是:当我删除同一个session下的一个数据后,再进行添加功能就会出现这个情况。
我的解决方式:在我做S2SH多表联合整合开发中,还是用的DAO层中两表建立关系的时候使用,User uu =(User) template.get(User.class, u.getUserId());uu.addCustomer(c);customerDAO.add(c);这个来解决不但可以解决延迟加载的异常,更加可以解决这个异常,但是这个方式还是有点不尽如人意的地方就是,出现在service层实现运用了template,而template一般运用于dao层。
原创粉丝点击