天天看點

Hibernate中集合映射配置

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>


           

繼續閱讀