簡體   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