2017-01-01 64 views
1

總結什麼我迄今發現通過不同的計算器帖子:休眠節省VS saveOrUpdate VS更新VS堅持

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. 

對於指定的標識符的實體:

保存():它會立即返回一個實體的標識符。由於標識符在調用保存之前已被分配給實體,所以插入不會立即觸發。它在會話刷新時被觸發。

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

回答

0

保存VS堅持 - 所有的方法應在交易

被稱爲保存返回ID,但堅持不。我們不能堅持分離的對象,因爲我們可以保存分離的對象。

更新vs合併 - 所有方法都應在Transaction中調用。

當我有分離的對象,我有一個持久性對象的主鍵然後如果我想持久分離的對象然後: - a。如果我們在分離的對象上使用更新,它將通過例外。 b。如果我們在分離的對象上使用合併,它會將字段的值複製到持久對象並在數據庫中更新。

如果我們沒有分離對象的相同主鍵的持久化對象,那麼我們可以使用update方法來使其持久化。

應該總是使用更好的合併方法。

得到VS負載

get方法總是打數據庫時被調用,其中作爲負載返回一個代理對象。當我們訪問該對象的字段時,它會碰到數據庫。如果我們不需要使用對象字段的數據,我們只需要該對象傳遞給另一個對象,那麼我們應該使用load而不是get方法。