tx.commit();
错误:Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
为什么 保存会有错误呢
当时找到的原话是:
The JDBC driver is telling Hibernate that it did not update any rows as a result of Hibernate 's INSERT or UPDATE. Hibernate reasonably enough thinks that this cannot be correct.
注意unsaved-value是个很重要的属性。Hibernate通过这个属性来判断一个对象应该save还是update,如果这个对象的id是unsaved-value的话,那说明这个对象不是 persistence object要save(insert);如果id是非unsaved-value的话,那说明这个对象是persistence object(数据库中已存在),只要update就行了。
你可以Debug跟一下你的代码,看看在
session.update(obj);
的时候,obj的主键的值是不是按你定义的那样,obj可以被hibernate认为是一个已经持久化在数据库里的对象