hiberate错误a different object with the same identifier value was already associated with

您所在的位置:网站首页 different有副词吗 hiberate错误a different object with the same identifier value was already associated with

hiberate错误a different object with the same identifier value was already associated with

2023-06-22 03:04| 来源: 网络整理| 查看: 265

错误信息: a different object with the same identifier value was already associated with

最近在工作中遇到这样一个异常,异常的原因很容易找到,就是看怎么去解决这个异常了 。 原因:正常情况一般调用hiberate端方法进行数据库操作时,是直接调用 getHibernateTemplate().update(user);,但在hibernate中同一个session里面有了两个相同标识但是是 不同实体时就会出现如标题的错误,

自己尝试用了网上搜索到的好多方法都不能解决。最后找到了两种解决方案:cleaning cloth

我用的是hibernate3.0。 方案一:在写完要一个完整的操作之后,比如,delete操作,调用this.getHibernateTemplate().evict(object),这样就可以把多余的对象给清除掉了。然后再执行需要的操作,如saveOrUpdate。

方案二:同样是在一个完整的操作之后,比如,delete操作,然后同时调用 this.getHibernateTemplate().flush();this.getHibernateTemplate().clear(); 来强迫执行,然后再执行需要的操作,如saveOrUpdate。(注意,这种方法要同时执行这两个方法,单独执行一个都无法达到预期目的)。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3