Hibernate如何保存结合属性?
Hibernate会将集合属性中的内容保存在一个从表中,并通过外键列与主表相关联。
映射集合属性的元素
<primitive-array/>:专门用于映射byte、short、int、long等基本数据类型的数组,不能用来映射Integer等包装类型
<array/>:用于映射数组集合属性
<list/>:用于映射list集合属性
<set/>:用于映射set集合属性
<map/>:用于映射map集合属性
<bay/>:用于映射无序集合
<idbag/>:用于映射无需集合,但为集合增加逻辑次序
外键列
因为集合属性都需要保存到另一个数据表中,所以保存集合属性的数据表必须包含一个外键列,用于参照到主表的主键列。
该外键列通过在<set/>、<list/>等元素中使用<key/>子元素来映射。
<key column="foreign_id" not-null="true" on-delete="noaction" unique="true" update="true" foreign-key="" property-ref=""/>
column:外键字段的列名
not-null:指定外键列是否具有非空约束
on-delete:指定外键列约束是否打开数据库级别的级联删除
unique:指定外键列是否具有唯一性约束
foreign-key:暂不清楚
property-ref:暂不清楚
索引列
在java中,List、数组使用整数作为集合元素的索引值,Map使用key作为索引值(key可以是基本数据类型、String、Date,也可以是自定义类型),所以在从表中存储数组、List、Map集合元素的同时,还必须存储一个索引列。
Set集合是无序的,没有显示的索引值,所以存储Ser集合时,无需存储索引列。
List、数组的索引列:
<list-index column="list_index" base="1"/>
column:索引列的名称
base:索引列的起始值
Map的索引列:
集合元素的类型
集合元素是基本数据类型及其包装类型、String、Date时,使用<element/>元素映射
<element column="str" type="string"/>
column:数据列的名称
type:集合元素的类型,只能是基本数据类型及其包装类型、String、Date
集合元素是复合类型时,使用<composite-element/>元素映射,该元素与<component/>元素的用法非常相似。
<composite-element/>中可以映射的数据类型
1、使用<property/>元素映射基本数据类型及其包装类型、String、Date
具体使用请参见《Hibernate映射文件之映射普通属性》
2、使用<nested-composite-element/>元素映射组件属性
3、使用<many-to-one/>来映射关联关系
请参见《Hibernate映射文件之关联关系映射》
集合元素是其他持久化对象的引用时,使用<one-to-many/>或<many-to-many/>元素映射
参见《Hibernate映射文件之关联关系映射》
1、用<primitive-array/>映射基本数据类型的数组
注意:此处的基本数据类型不包括基本数据类型的包装类型
<primitive-array name="primitiveArray" table="primitive_array"> <key column="foreign_id" not-null="true"/> <list-index column="list_index" base="1"/> <element column="number" type="java.lang.Integer"/> </primitive-array>
2、用<array/>映射任意类型的数组
1)、数组元素为基本数据类型及其包装类型、String类型、Date类型
<array name="intArray" table="int_array"> <key column="foreign_id"/> <list-index column="array_index"/> <element column="number" type="java.lang.Integer"/> </array>
<array name="strArray" table="str_array"> <key column="foreign_id"/> <list-index column="array_index"/> <element column="str" type="string"/> </array>
<array name="dateArray" table="date_array"> <key column="foreign_id"/> <list-index column="array_index"/> <element column="date_time" type="timestamp"/> </array>
2)、数组元素为组件
3)、数组元素为其他持久化对象
请参见《Hibernate映射文件之关联关系映射》
3、用<list/>映射List集合属性
1)、数组元素为基本数据类型及其包装类型、String类型、Date类型
<list name="intList" table="int_list" lazy="false"> <key column="foreign_id"/> <list-index column="list_index"/> <element column="number" type="java.lang.Integer"/> </list>
<list name="strList" table="str_list" lazy="false"> <key column="foreign_id"/> <list-index column="list_index"/> <element column="str" type="string"/> </list>
<list name="dateList" table="date_list" lazy="false"> <key column="foreign_id"/> <list-index column="list_index"/> <element column="date_time" type="timestamp"/> </list>
2)、集合元素为组件
3)、集合元素为其他持久化对象
请参见《Hibernate映射文件之关联关系映射》
4、用<set/>映射Set集合属性
1)、数组元素为基本数据类型及其包装类型、String类型、Date类型
<set name="intSet" table="int_set" lazy="true"> <key column="foreing_id"/> <element column="number" type="integer"/> </set>
<set name="strSet" table="str_set" lazy="true"> <key column="foreign_id"/> <element column="str" type="string"/> </set>
<set name="dateSet" table="date_set" lazy="true"> <key column="foreign_id"/> <element column="date_time" type="timestamp"/> </set>
2)、集合元素为组件
3)、集合元素为其他持久化对象
请参见《Hibernate映射文件之关联关系映射》
5、用<map/>映射Map集合属性
1)、数组元素为基本数据类型及其包装类型、String类型、Date类型
<map name="intMap" table="int_map"> <key column="foreign_id"/> <map-key column="map_key" type="integer"/> <element column="number" type="integer"/> </map>
<map name="strMap" table="str_map"> <key column="foreign_key"/> <map-key column="map_key" type="string"/> <element column="str" type="string"/> </map>
<map name="dateMap" table="date_map"> <key column="foreign_id"/> <map-key column="map_key" type="string"/> <element column="date_time" type="timestamp"/> </map>
2)、集合元素为组件
3)、集合元素为其他持久化对象
请参见《Hibernate映射文件之关联关系映射》