a different object with the same identifier value was already associated with the session 问题解决
来源:互联网 发布:珂莱欧淘宝旗舰店 编辑:程序博客网 时间:2024/06/04 19:31
在学习dorado的多对一关联操作的时候遇到了下面的问题
使用dorado7控件+hibernate+spring下
对象:联系人 邮件, 一个联系人对应多封邮件
操作多封邮件数据的时候出现问题:
a different object with the same identifier value was already associated with the session
大致原因:根本翻译过来是同个session中出现相同标识的不同实体
举个列子:你通过控件添加了两条数据,然后执行保存操作 ,控件下面会new 两条message,两条的message的id是一样的
可以参考:http://fatkun.com/2011/04/org-hibernate-nonuniqueobjectexception.html
解决方法:一开始网上找了许多,大致的解决方法都是在session上作的操作
方法一: session.clean()。但是在clean后进行持久化操作后可能有问题
方法二:session.refresh(object) object必须是数据库已有数据的对象
方法三:session.merge(object) 可参考http://blog.csdn.net/lang_man_xing/article/details/7572964
方法看起来挺多,但是我这里前段使用的是控件,服务端是封装好的东西,要找session操作很难。
最后经同事提醒之后,发现我在entity里面的SlMessage的id没有自动生成的标签,我是从数据库的表来生成这个实体类的,表里面id是自增长的,但是在实体类里面没
有这个标签。
最后在实体类的getMessageId()上面贴上@GeneratedValue(strategy=GenerationType.IDENTITY).
strategy里面有四种格式 TABLE:使用一个特定的数据库表格来保存主键。
SEQUENCE:根据底层数据库的序列来生成主键,条件是数据库支持序列。
IDENTITY:主键由数据库自动生成(主要是自动增长型)
AUTO:主键由程序控制。
阅读全文
0 0
- a different object with the same identifier value was already associated with the session 问题解决
- 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错误
- 解决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的解决办法
- 解决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
- 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错误
- 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
- 再次碰到: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
- 解决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错误
- HibernateSystemException: 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
- a different object with the same identifier value was already associated with the session
- struts2常用结果类型
- linux在当前目录下根据文件名查找文件
- LOG4J 在日期的前提下生成多个大小一致的日志(同一天生成多个日志,日志大小可配置)
- react常见坑
- MySQL系列—修改多张分表的相同字段
- a different object with the same identifier value was already associated with the session 问题解决
- jmeter配置
- Android性能优化:ViewStub
- ios 建立pods库发布代码到cocoapods上
- 万圣节派对
- SNAT与DNAT
- Lua 执行命令行获取文件MD5值
- JUC集合-06之 ConcurrentSkipListSet
- gulp使用