日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
解讀Hibernate高級集合映射

本文主要介紹Hibernate高級集合映射(Advanced collection mappings),Hibernate高級集合映射主要分為有序集合、雙向關(guān)聯(lián)、雙向關(guān)聯(lián),涉及有序集合類、 三重關(guān)聯(lián)(Ternary associations)、使用

成都創(chuàng)新互聯(lián)公司長期為1000多家客戶提供的網(wǎng)站建設(shè)服務(wù),團隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為趙縣企業(yè)提供專業(yè)的成都做網(wǎng)站、網(wǎng)站制作,趙縣網(wǎng)站改版等技術(shù)服務(wù)。擁有十載豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。

1. 有序集合(Sorted collections)

Hibernate高級集合映射支持實現(xiàn)java.util.SortedMap和java.util.SortedSet的集合。你必須在映射文件中指定一個比較器:

 
 
 
  1.  name="aliases"   
  2.             table="person_aliases"   
  3.             sort="natural"> 
  4.      column="person"/> 
  5.      column="name" type="string"/> 
  6.  
  7.  
  8.  name="holidays" sort="my.custom.HolidayComparator"> 
  9.      column="year_id"/> 
  10.      column="hol_name" type="string"/> 
  11.      column="hol_date" type="date"/> 
  12.  

sort屬性中允許的值包括unsorted,natural和某個實現(xiàn)了java.util.Comparator的類的名稱。

分類集合的行為事實上象java.util.TreeSet或者java.util.TreeMap。

如果你希望數(shù)據(jù)庫自己對集合元素排序,可以利用set,bag或者map映射中的order-by屬性。這個解決方案只能在jdk1.4或者更高的jdk版本中才可以實現(xiàn)(通過LinkedHashSet或者 LinkedHashMap實現(xiàn))。 它是在SQL查詢中完成排序,而不是在內(nèi)存中。

 
 
 
  1.  name="aliases" table="person_aliases" order-by="lower(name) asc"> 
  2.      column="person"/> 
  3.      column="name" type="string"/> 
  4.  
  5.  
  6.  name="holidays" order-by="hol_date, hol_name"> 
  7.      column="year_id"/> 
  8.      column="hol_name" type="string"/> 
  9.      column="hol_date" type="date"/> 
  10.  

注意: 這個order-by屬性的值是一個SQL排序子句而不是HQL的!

關(guān)聯(lián)還可以在運行時使用集合filter()根據(jù)任意的條件來排序。

 
 
 
  1. ssortedUsers = s.createFilter( group.getUsers(), "order by this.name" ).list(); 

2. 雙向關(guān)聯(lián)(Bidirectional associations)

雙向關(guān)聯(lián)允許通過關(guān)聯(lián)的任一端訪問另外一端。在Hibernate中, 支持兩種類型的雙向關(guān)聯(lián):

◆一對多(one-to-many)
Set或者bag值在一端, 單獨值(非集合)在另外一端

◆多對多(many-to-many)
兩端都是set或bag值

要建立一個雙向的多對多關(guān)聯(lián),只需要映射兩個many-to-many關(guān)聯(lián)到同一個數(shù)據(jù)庫表中,并再定義其中的一端為inverse(使用哪一端要根據(jù)你的選擇,但它不能是一個索引集合)。

這里有一個many-to-many的雙向關(guān)聯(lián)的例子;每一個category都可以有很多items,每一個items可以屬于很多categories:

 
 
 
  1.  name="Category"> 
  2.      name="id" column="CATEGORY_ID"/> 
  3.     ...  
  4.      name="items" table="CATEGORY_ITEM"> 
  5.          column="CATEGORY_ID"/> 
  6.          class="Item" column="ITEM_ID"/> 
  7.      
  8.  
  9.  
  10.  name="Item"> 
  11.      name="id" column="CATEGORY_ID"/> 
  12.     ...  
  13.  
  14.      
  15.      name="categories" table="CATEGORY_ITEM" inverse="true"> 
  16.          column="ITEM_ID"/> 
  17.          class="Category" column="CATEGORY_ID"/> 
  18.      
  19.  

如果只對關(guān)聯(lián)的反向端進行了改變,這個改變不會被持久化。 這表示Hibernate為每個雙向關(guān)聯(lián)在內(nèi)存中存在兩次表現(xiàn),一個從A連接到B,另一個從B連接到A。如果你回想一下Java對象模型,我們是如何在Java中創(chuàng)建多對多關(guān)系的,這可以讓你更容易理解:

 
 
 
  1. category.getItems().add(item);          // The category now "knows" about the relationship  
  2. item.getCategories().add(category);     // The item now "knows" about the relationship  
  3.  
  4. session.persist(item);                   // The relationship won''t be saved!  
  5. session.persist(category);               // The relationship will be saved 

非反向端用于把內(nèi)存中的表示保存到數(shù)據(jù)庫中。

要建立一個一對多的雙向關(guān)聯(lián),你可以通過把一個一對多關(guān)聯(lián),作為一個多對一關(guān)聯(lián)映射到到同一張表的字段上,并且在"多"的那一端定義inverse="true"。

 
 
 
  1.  name="Parent"> 
  2.      name="id" column="parent_id"/> 
  3.     ....  
  4.      name="children" inverse="true"> 
  5.          column="parent_id"/> 
  6.          class="Child"/> 
  7.      
  8.  
  9.  
  10.  name="Child"> 
  11.      name="id" column="child_id"/> 
  12.     ....  
  13.      name="parent"   
  14.         class="Parent"   
  15.         column="parent_id" 
  16.         not-null="true"/> 
  17.  

在“一”這一端定義inverse="true"不會影響級聯(lián)操作,二者是正交的概念!

3. 雙向關(guān)聯(lián),涉及有序集合類

對于有一端是或者的雙向關(guān)聯(lián),需要加以特別考慮。假若子類中的一個屬性映射到索引字段,沒問題,我們?nèi)匀豢梢栽诩项愑成渖鲜褂胕nverse="true":

 
 
 
  1.  name="Parent"> 
  2.      name="id" column="parent_id"/> 
  3.     ....  
  4.      name="children" inverse="true"> 
  5.          column="parent_id"/> 
  6.          column="name"   
  7.             type="string"/> 
  8.          class="Child"/> 
  9.      
  10.  
  11.  
  12.  name="Child"> 
  13.      name="id" column="child_id"/> 
  14.     ....  
  15.      name="name"   
  16.         not-null="true"/> 
  17.      name="parent"   
  18.         class="Parent"   
  19.         column="parent_id" 
  20.         not-null="true"/> 
  21.  

但是,假若子類中沒有這樣的屬性存在,我們不能認為這個關(guān)聯(lián)是真正的雙向關(guān)聯(lián)(信息不對稱,在關(guān)聯(lián)的一端有一些另外一端沒有的信息)。在這種情況下,我們不能使用inverse="true"。我們需要這樣用:

 
 
 
  1.  name="Parent"> 
  2.      name="id" column="parent_id"/> 
  3.     ....  
  4.      name="children"> 
  5.          column="parent_id" 
  6.             not-null="true"/> 
  7.          column="name"   
  8.             type="string"/> 
  9.          class="Child"/> 
  10.      
  11.  
  12.  
  13.  name="Child"> 
  14.      name="id" column="child_id"/> 
  15.     ....  
  16.      name="parent"   
  17.         class="Parent"   
  18.         column="parent_id" 
  19.         insert="false" 
  20.         update="false" 
  21.         not-null="true"/> 
  22.  

注意在這個映射中,關(guān)聯(lián)中集合類"值"一端負責來更新外鍵.TODO: Does this really result in some unnecessary update statements?

4. 三重關(guān)聯(lián)(Ternary associations)

有三種可能的途徑來映射一個三重關(guān)聯(lián)。***種是使用一個Map,把一個關(guān)聯(lián)作為其索引:

 
 
 
  1.  name="contracts"> 
  2.      column="employer_id" not-null="true"/> 
  3.      column="employee_id" class="Employee"/> 
  4.      class="Contract"/> 
  5.  
  6.  name="connections"> 
  7.      column="incoming_node_id"/> 
  8.      column="outgoing_node_id" class="Node"/> 
  9.      column="connection_id" class="Connection"/> 
  10.  

第二種方法是簡單的把關(guān)聯(lián)重新建模為一個實體類。這使我們最經(jīng)常使用的方法。

***一種選擇是使用復合元素,我們會在后面討論

5. 使用

如果你完全信奉我們對于“聯(lián)合主鍵(composite keys)是個壞東西”,和“實體應該使用(無機的)自己生成的代用標識符(surrogate keys)”的觀點,也許你會感到有一些奇怪,我們目前為止展示的多對多關(guān)聯(lián)和值集合都是映射成為帶有聯(lián)合主鍵的表的!現(xiàn)在,這一點非常值得爭辯;看上去一個單純的關(guān)聯(lián)表并不能從代用標識符中獲得什么好處(雖然使用組合值的集合可能會獲得一點好處)。不過,Hibernate提供了一個(一點點試驗性質(zhì)的)功能,讓你把多對多關(guān)聯(lián)和值集合應得到一個使用代用標識符的表去。

屬性讓你使用bag語義來映射一個List (或Collection)。

 
 
 
  1.  name="lovers" table="LOVERS"> 
  2.      column="ID" type="long"> 
  3.          class="sequence"/> 
  4.      
  5.      column="PERSON1"/> 
  6.      column="PERSON2" class="Person" fetch="join"/> 
  7.  

你可以理解,人工的id生成器,就好像是實體類一樣!集合的每一行都有一個不同的人造關(guān)鍵字。但是,Hibernate沒有提供任何機制來讓你取得某個特定行的人造關(guān)鍵字。

注意的更新性能要比普通的高得多!Hibernate可以有效的定位到不同的行,分別進行更新或刪除工作,就如同處理一個list, map或者set一樣。

在目前的實現(xiàn)中,還不支持使用identity標識符生成器策略來生成集合的標識符。

【編輯推薦】

  1. Hibernate樂觀并發(fā)控制
  2. Hibernate傳播性持久化攻略
  3. 深入了解Hibernate自動狀態(tài)檢測
  4. 簡單學會Hibernate對象持久化
  5. 分析Hibernate自增主鍵

文章題目:解讀Hibernate高級集合映射
鏈接分享:http://www.dlmjj.cn/article/dpsphce.html