List
<class name="Person" table="person_inf">
<!-- 映射标识属性 -->
<id name="id" column="person_id">
<!-- 指定主键生成器策略 -->
<generator class="identity"/>
</id>
<!-- 映射普通属性 -->
<property name="name" type="string"/>
<property name="age" type="int"/>
<!-- 映射List集合属性 -->
<list name="schools" table="school">
<!-- 映射集合属性数据表的外键列 -->
<key column="person_id" not-null="true"/>
<!-- 映射集合属性数据表的集合索引列 -->
<list-index column="list_order"/>
<!-- 映射保存集合元素的数据列 -->
<element type="string" column="school_name"/>
</list>
</class>
Set
<class name="Person" table="person_inf">
<!-- 映射标识属性 -->
<id name="id" column="person_id">
<!-- 指定主键生成器策略 -->
<generator class="identity"/>
</id>
<!-- 映射普通属性 -->
<property name="name" type="string"/>
<property name="age" type="int"/>
<!-- 映射Set集合属性 -->
<set name="schools" table="school">
<!-- 映射集合属性数据表的外键列 -->
<key column="person_id" not-null="true"/>
<!-- 映射保存集合元素的数据列,增加非空约束 -->
<element type="string" column="school_name"
not-null="true"/>
</set>
</class>
List 集合的元素有顺序,而Set的集合的元素没有顺序。
注意:映射Set集合属性时,如果<element..../>元素包括not-nul=“true”属性,则集合属性表以关联持久化类的外键列和元素列作为联合主键,否则该表没有主键。
但List集合属性的表总是以外键列和元素索引列作为联合主键。
bag: <bag ../>元素即可以映射List集合属性,也可以映射Set集合属性,甚至可以映射Collection集合属性。
只需要〈key .../〉元素来映射关联的外键列,而使用〈element../>元素来映射集合属性列
<class name="Person" table="person_inf">
<!-- 映射标识属性 -->
<id name="id" column="person_id">
<!-- 指定主键生成器策略 -->
<generator class="identity"/>
</id>
<!-- 映射普通属性 -->
<property name="name" type="string"/>
<property name="age" type="int"/>
<!-- 使用bag元素映射集合属性 -->
<bag name="schools" table="school">
<!-- 映射集合属性数据表的外键列 -->
<key column="person_id" not-null="true"/>
<!-- 映射保存集合元素的数据列 -->
<element type="string" column="school_name"
not-null="true"/>
</bag>
</class>
Map :以外键列和key列作为联合主键
<class name="Person" table="person_inf">
<!-- 映射标识属性 -->
<id name="id" column="person_id">
<!-- 指定主键生成器策略 -->
<generator class="identity"/>
</id>
<!-- 映射普通属性 -->
<property name="name" type="string"/>
<property name="age" type="int"/>
<!-- 映射Map集合属性 -->
<map name="scores" table="score">
<!-- 映射集合属性数据表的外键列 -->
<key column="person_id" not-null="true"/>
<!-- 映射集合属性数据表的Map key列 -->
<map-key column="subject" type="string"/>
<!-- 映射保存集合元素的数据列 -->
<element column="grade" type="float"/>
</map>
</class>
通常List,Map集合性能较高,而Set则紧随其后。
有序集合映射:支持SortedSet和SortedMap两个有序集合
<class name="Person" table="person_inf">
<!-- 映射标识属性 -->
<id name="id" column="person_id">
<!-- 指定主键生成器策略 -->
<generator class="identity"/>
</id>
<!-- 映射普通属性 -->
<property name="name" type="string"/>
<property name="age" type="int"/>
<!-- 映射SortedSet集合属性,使用自然排序
增加sort="natural"定义-->
<set name="trainings" table="training"
sort="natural">
<!-- 映射集合属性数据表的外键列 -->
<key column="person_id" not-null="true"/>
<!-- 映射保存集合元素的数据列,增加非空约束 -->
<element type="string" column="training_name"
not-null="true"/>
</set>
</class>
或者
<!-- 映射Set集合属性,指定在内存中排序-->
<set name="trainings" table="training"
order-by="training desc">
<!-- 映射集合属性数据表的外键列 -->
<key column="person_id" not-null="true"/>
<!-- 映射保存集合元素的数据列,增加非空约束 -->
<element type="string" column="training_name"
not-null="true"/>
</set>