本篇內(nèi)容主要講解“Hibernate高級集合映射是什么”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“Hibernate高級集合映射是什么”吧!
創(chuàng)新互聯(lián)專注于丹鳳網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠為您提供丹鳳營銷型網(wǎng)站建設(shè),丹鳳網(wǎng)站制作、丹鳳網(wǎng)頁設(shè)計(jì)、丹鳳網(wǎng)站官網(wǎng)定制、微信小程序服務(wù),打造丹鳳網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供丹鳳網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。
Hibernate高級集合映射主要分為有序集合、雙向關(guān)聯(lián)、雙向關(guān)聯(lián),涉及有序集合類、 三重關(guān)聯(lián)(Ternary associations)、使用<idbag>。
1. 有序集合(Sorted collections)
Hibernate高級集合映射支持實(shí)現(xiàn)java.util.SortedMap和java.util.SortedSet的集合。你必須在映射文件中指定一個(gè)比較器:
<set name="aliases" table="person_aliases" sort="natural"> <key column="person"/> <element column="name" type="string"/> </set> <map name="holidays" sort="my.custom.HolidayComparator"> <key column="year_id"/> <map-key column="hol_name" type="string"/> <element column="hol_date" type="date"/> </map>
sort屬性中允許的值包括unsorted,natural和某個(gè)實(shí)現(xiàn)了java.util.Comparator的類的名稱。
分類集合的行為事實(shí)上象java.util.TreeSet或者java.util.TreeMap。
如果你希望數(shù)據(jù)庫自己對集合元素排序,可以利用set,bag或者map映射中的order-by屬性。這個(gè)解決方案只能在jdk1.4或者更高的jdk版本中才可以實(shí)現(xiàn)(通過LinkedHashSet或者 LinkedHashMap實(shí)現(xiàn))。 它是在SQL查詢中完成排序,而不是在內(nèi)存中。
<set name="aliases" table="person_aliases" order-by="lower(name) asc"> <key column="person"/> <element column="name" type="string"/> </set> <map name="holidays" order-by="hol_date, hol_name"> <key column="year_id"/> <map-key column="hol_name" type="string"/> <element column="hol_date" type="date"/> </map>
注意:這個(gè)order-by屬性的值是一個(gè)SQL排序子句而不是HQL的!
關(guān)聯(lián)還可以在運(yùn)行時(shí)使用集合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值在一端, 單獨(dú)值(非集合)在另外一端
◆多對多(many-to-many)
兩端都是set或bag值
要建立一個(gè)雙向的多對多關(guān)聯(lián),只需要映射兩個(gè)many-to-many關(guān)聯(lián)到同一個(gè)數(shù)據(jù)庫表中,并再定義其中的一端為inverse(使用哪一端要根據(jù)你的選擇,但它不能是一個(gè)索引集合)。
這里有一個(gè)many-to-many的雙向關(guān)聯(lián)的例子;每一個(gè)category都可以有很多items,每一個(gè)items可以屬于很多categories:
<class name="Category"> <id name="id" column="CATEGORY_ID"/> ... <bag name="items" table="CATEGORY_ITEM"> <key column="CATEGORY_ID"/> <many-to-many class="Item" column="ITEM_ID"/> </bag> </class> <class name="Item"> <id name="id" column="CATEGORY_ID"/> ... <!-- inverse end --> <bag name="categories" table="CATEGORY_ITEM" inverse="true"> <key column="ITEM_ID"/> <many-to-many class="Category" column="CATEGORY_ID"/> </bag> </class>
如果只對關(guān)聯(lián)的反向端進(jìn)行了改變,這個(gè)改變不會(huì)被持久化。 這表示Hibernate為每個(gè)雙向關(guān)聯(lián)在內(nèi)存中存在兩次表現(xiàn),一個(gè)從A連接到B,另一個(gè)從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ù)庫中。
要建立一個(gè)一對多的雙向關(guān)聯(lián),你可以通過把一個(gè)一對多關(guān)聯(lián),作為一個(gè)多對一關(guān)聯(lián)映射到到同一張表的字段上,并且在"多"的那一端定義inverse="true"。
<class name="Parent"> <id name="id" column="parent_id"/> .... <set name="children" inverse="true"> <key column="parent_id"/> <one-to-many class="Child"/> </set> </class> <class name="Child"> <id name="id" column="child_id"/> .... <many-to-one name="parent" class="Parent" column="parent_id" not-null="true"/> </class>
在“一”這一端定義inverse="true"不會(huì)影響級聯(lián)操作,二者是正交的概念!
3. 雙向關(guān)聯(lián),涉及有序集合類
對于有一端是<list>或者<map>的雙向關(guān)聯(lián),需要加以特別考慮。假若子類中的一個(gè)屬性映射到索引字段,沒問題,我們?nèi)匀豢梢栽诩项愑成渖鲜褂胕nverse="true":
<class name="Parent"> <id name="id" column="parent_id"/> .... <map name="children" inverse="true"> <key column="parent_id"/> <map-key column="name" type="string"/> <one-to-many class="Child"/> </map> </class> <class name="Child"> <id name="id" column="child_id"/> .... <property name="name" not-null="true"/> <many-to-one name="parent" class="Parent" column="parent_id" not-null="true"/> </class>
但是,假若子類中沒有這樣的屬性存在,我們不能認(rèn)為這個(gè)關(guān)聯(lián)是真正的雙向關(guān)聯(lián)(信息不對稱,在關(guān)聯(lián)的一端有一些另外一端沒有的信息)。在這種情況下,我們不能使用inverse="true"。我們需要這樣用:
<class name="Parent"> <id name="id" column="parent_id"/> .... <map name="children"> <key column="parent_id" not-null="true"/> <map-key column="name" type="string"/> <one-to-many class="Child"/> </map> </class> <class name="Child"> <id name="id" column="child_id"/> .... <many-to-one name="parent" class="Parent" column="parent_id" insert="false" update="false" not-null="true"/> </class>
注意在這個(gè)映射中,關(guān)聯(lián)中集合類"值"一端負(fù)責(zé)來更新外鍵.TODO: Does this really result in some unnecessary update statements?
4. 三重關(guān)聯(lián)(Ternary associations)
有三種可能的途徑來映射一個(gè)三重關(guān)聯(lián)。***種是使用一個(gè)Map,把一個(gè)關(guān)聯(lián)作為其索引:
<map name="contracts"> <key column="employer_id" not-null="true"/> <map-key-many-to-many column="employee_id" class="Employee"/> <one-to-many class="Contract"/> </map> <map name="connections"> <key column="incoming_node_id"/> <map-key-many-to-many column="outgoing_node_id" class="Node"/> <many-to-many column="connection_id" class="Connection"/> </map>
第二種方法是簡單的把關(guān)聯(lián)重新建模為一個(gè)實(shí)體類。這使我們最經(jīng)常使用的方法。
***一種選擇是使用復(fù)合元素,我們會(huì)在后面討論
5. 使用<idbag>
如果你完全信奉我們對于“聯(lián)合主鍵(composite keys)是個(gè)壞東西”,和“實(shí)體應(yīng)該使用(無機(jī)的)自己生成的代用標(biāo)識符(surrogate keys)”的觀點(diǎn),也許你會(huì)感到有一些奇怪,我們目前為止展示的多對多關(guān)聯(lián)和值集合都是映射成為帶有聯(lián)合主鍵的表的!現(xiàn)在,這一點(diǎn)非常值得爭辯;看上去一個(gè)單純的關(guān)聯(lián)表并不能從代用標(biāo)識符中獲得什么好處(雖然使用組合值的集合可能會(huì)獲得一點(diǎn)好處)。不過,Hibernate提供了一個(gè)(一點(diǎn)點(diǎn)試驗(yàn)性質(zhì)的)功能,讓你把多對多關(guān)聯(lián)和值集合應(yīng)得到一個(gè)使用代用標(biāo)識符的表去。
<idbag> 屬性讓你使用bag語義來映射一個(gè)List (或Collection)。
<idbag name="lovers" table="LOVERS"> <collection-id column="ID" type="long"> <generator class="sequence"/> </collection-id> <key column="PERSON1"/> <many-to-many column="PERSON2" class="Person" fetch="join"/> </idbag>
你可以理解,<idbag>人工的id生成器,就好像是實(shí)體類一樣!集合的每一行都有一個(gè)不同的人造關(guān)鍵字。但是,Hibernate沒有提供任何機(jī)制來讓你取得某個(gè)特定行的人造關(guān)鍵字。
注意<idbag>的更新性能要比普通的<bag>高得多!Hibernate可以有效的定位到不同的行,分別進(jìn)行更新或刪除工作,就如同處理一個(gè)list, map或者set一樣。
在目前的實(shí)現(xiàn)中,還不支持使用identity標(biāo)識符生成器策略來生成<idbag>集合的標(biāo)識符。
到此,相信大家對“Hibernate高級集合映射是什么”有了更深的了解,不妨來實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
本文名稱:Hibernate高級集合映射是什么
文章鏈接:http://jinyejixie.com/article36/pgsspg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)公司、網(wǎng)站營銷、網(wǎng)站策劃、
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)