http://www.tkk7.com/jinfeng_wang/archive/2005/04/11/3132.html
Mapping collections of value types (from 《hibernate in action》chapter 6)
這里所舉的例子,都是基于這樣的一個情景,Item中包含有Images。
<set name="images" lazy="true" table="ITEM_IMAGE">
<key column="ITEM_ID"/>
<element type="string" column="FILENAME" not-null="true"/>
</set>
這里的<key>定義了外鍵ITEM_ID,<elememt>則聲明了集合的內部數據元素。<set>定義的集合中不可以有重復的數據。
<idbag name="images" lazy="true" table="ITEM_IMAGE">
<collection-id type="long" column="ITEM_IMAGE_ID">
<generator class="sequence"/>
</collection-id>
<key column="ITEM_ID"/>
<element type="string" column="FILENAME" not-null="true"/>
</idbag>
ITEM_IMAGE_ID為主鍵,而ITEM_ID則為外鍵,<element>元素不變,這就允許集合中的數據重復。
<list name="images" lazy="true" table="ITEM_IMAGE">
<key column="ITEM_ID"/>
<index column="POSITION"/>
<element type="string" column="FILENAME" not-null="true"/>
</list>
這里為ITEM_IMAGE表添加了index,整個表的主鍵是ITEM_ID和POSITION構成的復合主鍵,它允許集合中的數據根據POSITION進行排序。
<map name="images"
lazy="true"
table="ITEM_IMAGE"
sort="natural">
<key column="ITEM_ID"/>
<index column="IMAGE_NAME" type="string"/>
<element type="string" column="FILENAME" not-null="true"/>
</map>
這里為ITEM_IMAGE表添加了index,整個表的主鍵是ITEM_ID和POSITION構成的復合主鍵,它允許集合中的數據根據sort指定的內容進行排序,這里允許用戶對sort進行定制。
<map name="images"
lazy="true"
table="ITEM_IMAGE"
order-by="IMAGE_NAME asc">
<key column="ITEM_ID"/>
<index column="IMAGE_NAME" type="string"/>
<element type="string" column="FILENAME" not-null="true"/>
</map>
這里采用的是orbder-by,注意sort和order-by的區別。Sort是在內存中使用comparator進行排序,而order-by則是在數據庫中使用SQL語句進行排序。
<set name="images"
lazy="true"
table="ITEM_IMAGE"
order-by="IMAGE_NAME asc">
<key column="ITEM_ID"/>
<composite-element class="Image">
<property name="name" column="IMAGE_NAME" not-null="true"/>
<property name="filename" column="FILENAME" not-null="true"/>
<property name="sizeX" column="SIZEX" not-null="true"/>
<property name="sizeY" column="SIZEY" not-null="true"/>
</composite-element>
</set>
這里定義了一個新的Image類,它不是實體類(entity class),而是一個簡單的值類型(value type),僅僅是父實體的一個component,其生命周期完全由包含其的父實體 (parent entity) ITEM控制。這里Image類中包含有數據ITEM_ID、 IMAGE_NAME、FILENAME、SIZEX、和SIZEY。
<set name="images"
lazy="true"
table="ITEM_IMAGE"
order-by="IMAGE_NAME asc">
<key column="ITEM_ID"/>
<composite-element class="Image">
<parent name="item"/>
<property name="name" column="IMAGE_NAME" not-null="true"/>
<property name="filename" column="FILENAME" not-null="true"/>
<property name="sizeX" column="SIZEX" not-null="true"/>
<property name="sizeY" column="SIZEY" not-null="true"/>
</composite-element>
</set>
前面定義的IMAGE類中,并不包含有對父實體ITEM的引用。也就是說,只可以從ITEM中導航到IMAGE中,但是在IMAGE無法得到ITEM。這可以通過<parent>實現。這樣IMAGE類中就含有一個數據成員item,指向其父實體。
但是,它是無法做到真正的雙向導航的。例如:通過SQL查詢直接得到IMAGE后,但是如同普通的值類型一樣,IMAGE對象的item是null,你無法從component如此直接得到其父對象,這必須由通過父-子關系的entity完成。