【问题标题】:Hibernate save vs saveOrUpdate vs update vs persistHibernate save vs saveOrUpdate vs update vs persist
【发布时间】:2017-01-01 04:01:52
【问题描述】:

总结我迄今为止通过不同的stackoverflow帖子发现的内容:

Persist
New
a) out of transaction: allowed. persist accomplished the task of saving both Parent and Child in one call.
b) in of transaction: allowed.
detached
a) out of transaction: allowed. will throw exception when trying to flush.
b) in of transaction: not allowed. will throw exception when trying to flush.
existing
a) out of transaction: allowed. will throw exception when trying to flush.
b) in of transaction: not allowed. will throw exception when trying to flush.

saveOrUpdate
new
a) out of transaction: allowed. will save.
b) in or transaction: allowed. will save.
detached
a) out of transaction: allowed. will update.
b) in of transaction: allowed. will update.
existing
a) out of transaction: allowed. will update.
b) in of transaction: allowed. will update.

save
new
a) out of transaction: allowed. will save. Not sure how can it be saved right away without transaction. save does not cascade to the child, i.e., only Parent is saved/inserted in the table.
b) in or transaction: allowed. will save.
detached
a) out of transaction: save() for a detached object will create a new row in the table.
b) in or transaction: not allowed. will throw exception.
existing
a) out of transaction: not sure what will happen.
b) in or transaction: not allowed. will throw exception.

update
new
a) out of transaction: not sure what will happen. should throw exception.
b) in or transaction: not sure what will happen. should throw exception.
detached
a) out of transaction: allowed. will update later during flush.
b) in or transaction: allowed. will update later during flush.
existing
a) out of transaction: allowed. will update later during flush.
b) in or transaction: allowed. will update later during flush.

merge
new
a) out of transaction: not sure what will happen.
b) in or transaction: if there is no persistent instance currently associated with the session, try to load it from the database, or create a new persistent instance.
detached
a) out of transaction: allowed. will update later during flush. return a copy.
b) in or transaction: allowed. will update later during flush. return a copy.
existing
a) out of transaction: allowed. will update later during flush. return a copy.
b) in or transaction: allowed. will update later during flush. return a copy.

对于具有指定标识符的实体:

save():它立即返回一个实体的标识符。由于在调用 save 之前标识符已经分配给实体,因此不会立即触发 insert。它在会话刷新时触发。

persist() :与保存相同。它还会在冲洗时触发插入。

请验证理解是否正确。

Hibernate persist() vs save() method Hibernate saveOrUpdate vs update vs save/persist Hibernate persist vs save What's the advantage of persist() vs save() in Hibernate? http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/objectstate.html

【问题讨论】:

    标签: java hibernate


    【解决方案1】:

    save vs persist -- 所有方法都应该在 Transaction 中调用

    保存返回 id 但不保存。我们不能持久化分离的对象,因为我们可以保存分离的对象。

    更新与合并——所有方法都应在事务中调用。

    当我有分离的对象并且我有一个相同主键的持久性对象时,如果我想持久化分离的对象,那么:- 一种。如果我们对分离的对象使用更新,它将通过异常。 湾。如果我们在分离对象上使用合并,它会将字段的值复制到持久对象并在数据库中更新。

    如果分离对象没有相同主键的持久化对象,则可以使用update方法使其持久化。

    应始终使用更好的合并方法。

    获取与加载

    get 方法在调用时总是命中数据库,其中 load 返回一个代理对象。当我们访问对象的字段时,它将访问数据库。如果我们不需要使用对象的字段数据,我们只需要将该对象传递给另一个对象,那么我们应该使用load而不是get方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-12
      • 2013-10-18
      • 1970-01-01
      • 2013-09-04
      • 2020-11-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多