新聞中心
本文主要介紹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的集合。你必須在映射文件中指定一個比較器:
name="aliases" - table="person_aliases"
- sort="natural">
column="person"/> column="name" type="string"/> column="year_id"/> column="hol_name" type="string"/> column="hol_date" type="date"/>
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)存中。
name="aliases" table="person_aliases" order-by="lower(name) asc"> column="person"/> column="name" type="string"/> column="year_id"/> column="hol_name" type="string"/> column="hol_date" type="date"/>
注意: 這個order-by屬性的值是一個SQL排序子句而不是HQL的!
關(guān)聯(lián)還可以在運行時使用集合filter()根據(jù)任意的條件來排序。
- 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:
name="Category"> name="id" column="CATEGORY_ID"/> - ...
name="items" table="CATEGORY_ITEM"> column="CATEGORY_ID"/> class="Item" column="ITEM_ID"/> name="Item"> name="id" column="CATEGORY_ID"/> - ...
name="categories" table="CATEGORY_ITEM" inverse="true"> column="ITEM_ID"/> class="Category" column="CATEGORY_ID"/>
如果只對關(guān)聯(lián)的反向端進行了改變,這個改變不會被持久化。 這表示Hibernate為每個雙向關(guān)聯(lián)在內(nèi)存中存在兩次表現(xiàn),一個從A連接到B,另一個從B連接到A。如果你回想一下Java對象模型,我們是如何在Java中創(chuàng)建多對多關(guān)系的,這可以讓你更容易理解:
- category.getItems().add(item); // The category now "knows" about the relationship
- item.getCategories().add(category); // The item now "knows" about the relationship
- session.persist(item); // The relationship won''t be saved!
- 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"。
name="Parent"> name="id" column="parent_id"/> - ....
name="children" inverse="true"> column="parent_id"/> class="Child"/> name="Child"> name="id" column="child_id"/> - ....
name="parent" - class="Parent"
- column="parent_id"
- not-null="true"/>
在“一”這一端定義inverse="true"不會影響級聯(lián)操作,二者是正交的概念!
3. 雙向關(guān)聯(lián),涉及有序集合類
對于有一端是或者
name="Parent"> name="id" column="parent_id"/> - ....
column="parent_id"/> column="name" - type="string"/>
class="Child"/> name="Child"> name="id" column="child_id"/> - ....
name="name" - not-null="true"/>
name="parent" - class="Parent"
- column="parent_id"
- not-null="true"/>
但是,假若子類中沒有這樣的屬性存在,我們不能認為這個關(guān)聯(lián)是真正的雙向關(guān)聯(lián)(信息不對稱,在關(guān)聯(lián)的一端有一些另外一端沒有的信息)。在這種情況下,我們不能使用inverse="true"。我們需要這樣用:
name="Parent"> name="id" column="parent_id"/> - ....
column="parent_id" - not-null="true"/>
column="name" - type="string"/>
class="Child"/> name="Child"> name="id" column="child_id"/> - ....
name="parent" - class="Parent"
- column="parent_id"
- insert="false"
- update="false"
- not-null="true"/>
注意在這個映射中,關(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)作為其索引:
column="employer_id" not-null="true"/> column="employee_id" class="Employee"/> class="Contract"/> column="incoming_node_id"/> column="outgoing_node_id" class="Node"/> column="connection_id" class="Connection"/>
第二種方法是簡單的把關(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)和值集合應得到一個使用代用標識符的表去。
name="lovers" table="LOVERS"> column="ID" type="long"> class="sequence"/> column="PERSON1"/> column="PERSON2" class="Person" fetch="join"/>
你可以理解,
注意
在目前的實現(xiàn)中,還不支持使用identity標識符生成器策略來生成
【編輯推薦】
- Hibernate樂觀并發(fā)控制
- Hibernate傳播性持久化攻略
- 深入了解Hibernate自動狀態(tài)檢測
- 簡單學會Hibernate對象持久化
- 分析Hibernate自增主鍵
文章題目:解讀Hibernate高級集合映射
鏈接分享:http://www.dlmjj.cn/article/dpsphce.html


咨詢
建站咨詢
