休眠 saveOrUpdate 行为 [英] Hibernate saveOrUpdate behavior

查看:22
本文介绍了休眠 saveOrUpdate 行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道当 session.saveOrUpdate() 被调用时,Hibernate 如何知道是插入还是更新数据库中的值?

Does anyone know how Hibernate knows whether to INSERT or to UPDATE a value in the database when session.saveOrUpdate() is called?

到目前为止,我只确定不依赖缓存中的信息,数据库中实体的存在是由主键决定的.

So far, I have only determined that it is not dependent on the information in the cache, and that the existence of the entity in the database is determined by the primary key.

推荐答案

当你使用 .saveOrUpdate() Hibernate 将检查对象是否是瞬态的(它没有标识符属性),如果是,它将通过生成标识符并将其分配给会话来使其持久化.如果对象已经有一个标识符,它将执行 .update().

When you use .saveOrUpdate() Hibernate will check if the object is transient (it has no identifier property) and if so it will make it persistent by generating it the identifier and assigning it to session. If the object has an identifier already it will perform .update().

来自文档:

saveOrUpdate() 执行以下操作:

saveOrUpdate() does the following:

  • 如果对象已经是持久化的在这个会话中,什么都不做
  • 如果另一个对象与会话具有相同的标识符,抛出异常
  • 如果对象没有标识符属性,保存()它
  • 如果对象的标识符具有分配给新的值实例化的对象,save()
  • 如果对象由版本"或时间戳",以及版本属性值相同分配给新的值实例化的对象,保存()它否则 update() 对象

这篇关于休眠 saveOrUpdate 行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆