您的位置:首页 > 其它

Hibernate学习笔记 merge()方法--update 之补充

2009-01-20 17:29 267 查看
补充1:
在DAO中,根据po的id先查询得到一个新的account对象,然后update po,(po必须是还没有被关联到session)

Account account = getById(po.getId());
getHibernateTemplate().update(po);


输出结果:
在调用update(po)这一行抛出Exception:
org.springframework.orm.hibernate3.HibernateSystemException: a different object with the same identifier value was already associated with the session: [org.sandbox.learn.sys.po.Account#1]; nested exception is org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [org.sandbox.learn.sys.po.Account#1]


补充2:
在同一个session中,获取了account对象,修改,在保存之前再次查询获取得到这个对象,两次得到的对象是否相同?

代码:
Account po = getById(getMaxId());
System.out.println("/r/n/tgetById: " + po.toDetailString());
po.setGroups(null);
System.out.println("/tupdated: " + po.toDetailString());
Account account = getById(po.getId());
System.out.println("/tgetById again: " + po.toDetailString());
System.out.println("/tis po == account ? " + (po==account));


输出结果:
getById: Account[6.account_06, groups[2.engineers 3.sales 1.administrators ]]
updated: Account[6.account_06, groups[]]
getById again: Account[6.account_06, groups[]]
is po == account ? true


补充3:
与上一篇相比,稍微修改一下测试逻辑
将第一步:从数据库获取一个account对象,修改为:
根据max(ID),mock一个对象,其内容与添加该对象时的mock结果相同
mock对象作为po传入,调用update方法
getHibernateTemplate().update(po);


输出结果:
Hibernate: update SYS_ACCOUNTS set ... where ID=?
2009-01-18 21:33:44,531 ERROR [301][org.hibernate.event.def.AbstractFlushingEventListener] Could not synchronize database state with session
org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1


原因:
mock过程中,没有设置id值 :-(
虚惊一场
再次验证:程序出现莫名其妙的错误,原因总是自己在某个不起眼的地方犯了个小错误
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐