Hibernate save vs saveOrUpdate vs update vs persist

问题描述 投票:1回答:1

总结我到目前为止通过不同的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() 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个回答
0
投票

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

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

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

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

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

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

得到vs负载

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

© www.soinside.com 2019 - 2024. All rights reserved.