在 05_传智播客hibernate教程_实体对象的三种状态与saveOrUpdate方法 1 中 我提出了一个疑问:
持久对象在session被删除后,那么这个对象是什么状态?
1 关于 delete Session.delete() will remove an object's state from the database. Your application, however,
can still hold a reference to a deleted object. It is best to think of delete() as making a persistent
instance, transient.
sess.delete(cat);
You can delete objects in any order, without risk of foreign key constraint violations. It is still
possible to violate a NOT NULL constraint on a foreign key column by deleting objects in the wrong
order, e.g. if you delete the parent, but forget to delete the children.
(删除一个瞬时对象是不会报错的)
比较特殊的是 删除后对象是有identifier值,但数据库没有这条记录
对这个对象进行save没有问题
update铁定报错
saveOrUpdate 铁定报错
原因见04_传智播客hibernate教程_Session接口及getloadpersist方法 2
所以在session删除后,还可能用这个对象进行数据库交互的 推荐将 identifier 设定成 unsaved-value值