繁体   English   中英

Hibernate save vs saveOrUpdate vs update vs persist

[英]Hibernate save vs saveOrUpdate vs update vs persist

总结我到目前为止通过不同的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():与save相同。 它还在冲洗时触发插入。

请验证理解是否正确。

Hibernate persist()vs save()方法 Hibernate saveOrUpdate vs update vs save / persist Hibernate persist vs save Hibernate 中persist()vs save()的优点是什么? http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/objectstate.html

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

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

update vs merge - 应在Transaction中调用所有方法。

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

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

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

得到vs负载

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM