Hibernate saveOrUpdate行为 [英] Hibernate saveOrUpdate behavior

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

问题描述

有人知道当调用 session.saveOrUpdate()时,Hibernate是否知道是否要INSERT或更新数据库中的值?

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().

http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/objectstate.html#objectstate-makingpersistent\">文档:

From the documentation:

saveOrUpdate()执行以下操作:

saveOrUpdate() does the following:


  • 如果对象在此会话中已经是持久的
    , / li>
  • 如果与
    会话关联的另一个对象具有相同的标识符,
    会抛出异常


  • 属性,save()it

  • 如果对象的标识符具有分配给新的
    实例化对象的
    值,save >
  • 如果对象由
    版本化,并且
    版本属性值与分配给新的
    实例化对象的
    值相同,则保存()it
    否则update()对象

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

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