亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 1346 | 回復: 0
打印 上一主題 下一主題

Hibernate的各種保存方式的區(qū)別1 [復制鏈接]

論壇徽章:
0
跳轉到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2008-12-19 11:24 |只看該作者 |倒序瀏覽
 hibernate對于對象的保存提供了太多的方法,他們之間有很多不同,這里細說一下,以便區(qū)別:
  一、預備知識:
  在所有之前,說明一下,對于hibernate,它的對象有三種狀態(tài),transient、persistent、detached
  下邊是常見的翻譯辦法:
  transient:瞬態(tài)或者自由態(tài)
  persistent:持久化狀態(tài)
  detached:脫管狀態(tài)或者游離態(tài)
  脫管狀態(tài)的實例可以通過調用save()、persist()或者saveOrUpdate()方法進行持久化。
  持久化實例可以通過調用 delete()變成脫管狀態(tài)。通過get()或load()方法得到的實例都是持久化狀態(tài)的。
  脫管狀態(tài)的實例可以通過調用 update()、0saveOrUpdate()、lock()或者replicate()進行持久化。
  save()和persist()將會引發(fā)SQL的INSERT,delete()會引發(fā)SQLDELETE,而update()或merge()會引發(fā)SQLUPDATE.對持久化(persistent)實例的修改在刷新提交的時候會被檢測到,它也會引起SQLUPDATE.saveOrUpdate()或者replicate()會引發(fā)SQLINSERT或者UPDATE
  二、save 和update區(qū)別
  把這一對放在第一位的原因是因為這一對是最常用的。
  save的作用是把一個新的對象保存
  update是把一個脫管狀態(tài)的對象保存
  三、update 和saveOrUpdate區(qū)別
  這個是比較好理解的,顧名思義,saveOrUpdate基本上就是合成了save和update引用hibernate reference中的一段話來解釋他們的使用場合和區(qū)別。
  通常下面的場景會使用update()或saveOrUpdate():
  程序在第一個session中加載對象
  該對象被傳遞到表現層
  對象發(fā)生了一些改動
  該對象被返回到業(yè)務邏輯層
  程序調用第二個session的update()方法持久這些改動
  saveOrUpdate()做下面的事:
  如果對象已經在本session中持久化了,不做任何事
  如果另一個與本session關聯的對象擁有相同的持久化標識(identifier),拋出一個異常
  如果對象沒有持久化標識(identifier)屬性,對其調用save()
  如果對象的持久標識(identifier)表明其是一個新實例化的對象,對其調用save()
  如果對象是附帶版本信息的(通過或) 并且版本屬性的值表明其是一個新實例化的對象,save()它。
  否則update() 這個對象
四,persist和save區(qū)別
  這個是最迷離的一對,表面上看起來使用哪個都行,在hibernate reference文檔中也沒有明確的區(qū)分他們。
  這里給出一個明確的區(qū)分。(可以跟進src看一下,雖然實現步驟類似,但是還是有細微的差別)

  這里參考
http://opensource.atlassian.com/projects/hibernate/browse/HHH-1682
中的一個說明:
I found that a lot of people have the same doubt. To help to solve this issue
  I'm quoting Christian Bauer:
  "In case anybody finds this thread……  
  persist() is well defined. It makes a transient instance persistent. However,
  it doesn't guarantee that the identifier value will be assigned to the persistent
[/url]

  instance immediately, the assignment might happen at flush time. The spec doesn't say

  that, which is the problem I have with persist()。
  persist() also guarantees that it will not execute an INSERT statement if it is called outside of transaction boundaries. This is useful in long-running conversations

  with an extended Session/persistence context.A method like persist() is required.
  save() does not guarantee the same, it returns an identifier, and if an INSERT
  has to be executed to get the identifier (e.g. "identity" generator, not "sequence"),
  this INSERT happens immediately, no matter if you are inside or outside of a transaction. This is not good in a long-running conversation with an extended Session/persistence context."

  簡單翻譯一下上邊的句子的主要內容:
  1,persist把一個瞬態(tài)的實例持久化,但是并"不保證"標識符被立刻填入到持久化實例中,標識符的填入可能被推遲到flush的時間。
  2,persist"保證",當它在一個transaction外部被調用的時候并不觸發(fā)一個Sql Insert,這個功能是很有用的,
中國網管聯盟bitsCN.com
  當我們通過繼承Session/persistence context來封裝一個長會話流程的時候,一個persist這樣的函數是需要的。

  3,save"不保證"第2條,它要返回標識符,所以它會立即執(zhí)行Sql insert,不管是不是在transaction內部還是外部



本文來自ChinaUnix博客,如果查看原文請點:[url]http://blog.chinaunix.net/u1/57965/showart_1729192.html
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復

  

北京盛拓優(yōu)訊信息技術有限公司. 版權所有 京ICP備16024965號-6 北京市公安局海淀分局網監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯網協會會員  聯系我們:huangweiwei@itpub.net
感謝所有關心和支持過ChinaUnix的朋友們 轉載本站內容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP