2012-01-11 49 views
1

我正在閱讀有關Spring3中的HibernateTemplate類。Spring hibernate模板類

在這裏,我看到了兩種方法更新和saveOrUpdate,我從文檔如

  • 更新

更新給定的持久化實例看,它與當前Hibernate的Session相關聯。

這裏「將它與當前的Hibernate Session關聯」是什麼意思?

  • saveOrUpdate

保存或更新給定的持續情況下,根據其ID(匹配配置的 「未保存值」?)。將實例與當前的Hibernate Session相關聯。

這裏「根據它的id(匹配配置的」unsaved-value「?)的含義是什麼?將實例與當前的Hibernate Session相關聯?

請幫我

在此先感謝

回答

1

簡單 - 如果具有相同ID的對象在數據庫中存在,它更新否則如果ID(主鍵)爲空,這樣可以節省它作爲一個新值 - 因此saveOrUpdate - 取決於它是否存在於數據庫中的值或新值。

ID匹配是通過將hbm下的hbm中的pojo ID與指定的列匹配來實現的。

saveOrUpdate()方法執行以下操作:

if the object is already persistent in this session, do nothing 

if another object associated with the session has the same identifier, throw an exception 

if the object has no identifier property, save() it 

if the object's identifier has the value assigned to a newly instantiated object, save() it 

if the object is versioned by a <version> or <timestamp>, and the version property value is the same value assigned to a newly instantiated object, save() it 

otherwise update() the object 

here

希望這有助於。

1

這裏「將它與當前Hibernate Session關聯」是什麼意思?

通過此Session檢索該實體後,將爲您提供最新的實體。其他Session s將不會看到更新,直到Session被刷新。

這是什麼意思「,根據其ID(匹配配置的‘未保存的價值’?)。

如果實體有一個ID,它是由saveOrUpdate視爲一個現有實體(在數據庫中),否則它是一個新的實體,「unsaved-value」是Hibernate將在一個新實體的saveOrUpdate上的id值,你可以將它設置爲負值。也可以在從Session後面檢索該實體時確定它是否是尚未保存在數據庫中的新實體或現有實體。

1

這裏什麼是「將它與當前休眠 會話相關聯」的含義?

意味着你的實體(瞬態或分離)將得到保存和準與會話(附/持續),所以從那時候起,會議將跟蹤該實體所做的任何更改,並減少需要數DML語句最後執行。

這裏是什麼「根據其ID(匹配配置 ‘未保存的價值意義’?),與當前的Hibernate 會議聯營實例」?

如果實體ID屬性具有值那麼得到更新,並重視通過會話其他的變化進一步跟蹤會話將插入到實體爲DB新的記錄,並關聯到會話。

請通過hibernate實體的實體instance states