集合映射:对应的类没有对象表示符属性,只能作为一个持久化类的属性使用。如果持久化类中有一个值类型的集合,那么就需要一张额外的数据库表来保存这个值类型的集合,这张表被称为集合表。
集合的类型有:<set>、<list>、<bag>、<map>、<primitive-array>
1、<set>:元素储存没有顺序不允许重复
示例:private Set<String> hobbies;//个人爱好属性
映射:<set name=”hobbies” table=”student_hobby”>
<key column=”student_id”/><!—key子元素:指定元素集合属性对应的表的外键-->
< element type=”string” column=”hobbies_name” not-null=”true”/><!—element子元素,映射集合内的元素-->
</set>
2、<list>在集合属性对应数据库表中用一个额外的索引列保存每个属性的位置
示例:private List<String> hobbies;//个人爱好属性
映射:<list name=”hobbies” table=”student_hobby”>
<key column=”student_id”/><!—key子元素:指定元素集合属性对应的表的外键-->
<list-index column=”posistion”><!--指定索引列-->
< element type=”string” column=”hobbies_name” not-null=”true”/><!—element子元素,映射集合内的元素-->
</list>
3、<bag>元素可能重复,但不能保存顺序
示例:private Collection<String> hobbies;//个人爱好属性
映射:<bag name=”hobbies” table=”student_hobby”>
<key column=”student_id”/><!—key子元素:指定元素集合属性对应的表的外键-->
< element type=”string” column=”hobbies_name” not-null=”true”/><!—element子元素,映射集合内的元素-->
</bag>
4、<map>可以映射为java.uitl.Map接口的属性,以键/值对的形式保存,也是无序的,也可以映射为java.uitl.SortMap接口的属性,元素可以按自然顺序排列
示例:private Map<Long,String> hobbies;//个人爱好属性
映射:<map name=”hobbies” table=”student_hobby”>
<key column=”student_id”/><!--key元素:指定元素集合属性对应的表的外键-->
<map-key column=”hobby_id” type=”long”> <!—map-key元素:指定map属性的键对应表中的列-->
<element type=”string” column=”hobby_name” not-null=”true”><!—element子元素,映射集合内的元素-->
</map>
5、<primitive>或<array>映射类型为数组的属性
示例:private SortedSet<String> hobbies;
映射:<set name=”hobbies” table=”student_hobby” sort=”natural”>
<key column=”student_id”/><!—key子元素:指定元素集合属性对应的表的外键-->
< element type=”string” column=”hobbies_name” not-null=”true”/><!—element子元素,映射集合内的元素-->
</set>