Hibernate 关联映射之集合映射

Hibernate的关联映射包括:

一对一(Persion - IDCard)

一对多(Department - Employee)

多对一(Employee - Department)

多对多(Teacher - Student)

组件映射(User - Name)

集合映射(Set、List、Map)

inverse和cascade(Employee - Department)

本文以集合映射为例:

集合映射包括:set、list、array、bag、map

一、set

语法:

<set name="" access="" batch-size="" cascade="" catalog=""
			 check="" collection-type="" embed-xml="true" fetch="join"
			 inverse="false" lazy="true" mutable="true" node=""
			 optimistic-lock="true" order-by="" outer-join="true"
			 persister="" schema="" sort="unsorted" subselect=""
			 table="" where="">
				 <cache usage="read-only" include="all" region=""/>
				 <comment></comment>
				 <composite-element class="" node=""></composite-element>
				 <element />
				 <filter name=""></filter>
				 <key></key>
				 <loader query-ref=""/>
				 <many-to-any id-type="" meta-type="">f</many-to-any>
				 <many-to-many></many-to-many>
				 <meta attribute=""></meta>
				 <one-to-many/>
				 <sql-delete></sql-delete>
				 <sql-delete-all></sql-delete-all>
				 <sql-insert></sql-insert>
				 <sql-update></sql-update>
				 <subselect></subselect>
				 <synchronize table=""/>
		</set>

其中:

1、cascade配置级联关系。用来说明当对主对象进行某种操作时是否自动对其关联的从对象也做操作,cascade值包括:

none,all,save-update,delete,lock,refresh,evict,replicate,persist,merge,delete-orphan。

缺省情况下是不做级联操作的。一般对多对一、多对多不设置级联,在一对一、一对多中设置级联。

2、inverse表示是否要放弃维护关联关系。在一对多和多对多的时候,两边是都可以维护关联关系的,为避免重复操作数据库,可以选择一边放弃维护关联关系。

inverse不能在有序的集合中使用,也就是说不能在list、array中使用。

inverse只在集合元素中可以使用,因为只有集合元素中才涉及到对象间的关联关系。

 例子:Department.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.cos.entity">
	<class name="Department">		
		<id name="id">
			<generator class="native"/>
		</id>
		<property name="name" column="name"/>
		<set name="emps">
			<key column="dept_id"/>
			<one-to-many class="Employee"/>
		</set>
	</class>
</hibernate-mapping>

其中:

set标签中的name是Department.java中的一对多字段empt,代表Employee.java这个对象

key中的column值代表映射到<one-to-many>标标签中对象的数据库的字段

one-to-many代表一对多所映射的对象

二、list

语法:

<list name="" access="" batch-size="" cascade="" catalog="" check=""
			  collection-type="" embed-xml="true" fetch="join" inverse="false"
			  lazy="true" mutable="true" node="" optimistic-lock="true" 
			  outer-join="true" persister="" schema="" subselect="" table=""
			  where="">
				  	<cache usage="read-only"/>
				  	<comment></comment>
				  	<composite-element class=""></composite-element>
				  	<element></element>
				  	<filter name=""></filter>
				  	<index></index>
				  	<key></key>
				  	<list-index></list-index>
				  	<loader query-ref=""/>
				  	<many-to-any id-type=""></many-to-any>
				  	<many-to-many></many-to-many>
				  	<meta attribute=""></meta>
				  	<one-to-many/>
				  	<sql-delete></sql-delete>
				  	<sql-delete-all></sql-delete-all>
				  	<sql-insert></sql-insert>
				  	<sql-update></sql-update>
				  	<subselect></subselect>
				  	<synchronize table=""/>
		</list>

例子:Department.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.cos.entity">
	<class name="Department">		
		<id name="id">
			<generator class="native"/>
		</id>
		<property name="name" column="name"/>
		<!--  set name="emps">
			<key column="dept_id"/>
			<one-to-many class="Employee"/>
		</set> -->
		<list name="emps">
			<key column="dept_id"/>
			<list-index column="order_id"/>
			<one-to-many class="Employee"/>
		</list>
	</class>
</hibernate-mapping>

 其中list标签中的name值是Department对象中的属性,一对多属性。

key中的column属性是一对多表中的字段,和本表中的id对应

list-index标签是记录加入list中的顺序。

one-to-many标签的class值是一对多中的多对象。

三、bag

语法:

<bag name="" access="" batch-size="" cascade="" catalog="" check=""
			  collection-type="" embed-xml="true" fetch="join" inverse="false"
			  lazy="true" mutable="true" node="" optimistic-lock="true" 
			  outer-join="true" persister="" schema="" subselect="" table=""
			  where="">
					<cache usage="read-only"/>
				  	<comment></comment>
				  	<composite-element class=""></composite-element>
				  	<element></element>
				  	<filter name=""></filter>
				  	<key></key>
				  	<loader query-ref=""/>
				  	<many-to-any id-type=""></many-to-any>
				  	<many-to-many></many-to-many>
				  	<meta attribute=""></meta>
				  	<one-to-many/>
				  	<sql-delete></sql-delete>
				  	<sql-delete-all></sql-delete-all>
				  	<sql-insert></sql-insert>
				  	<sql-update></sql-update>
				  	<subselect></subselect>
				  	<synchronize table=""/>
		</bag>

 例子:Department.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.cos.entity">
	<class name="Department">		
		<id name="id">
			<generator class="native"/>
		</id>
		<property name="name" column="name"/>
		<bag name="emps">
			<key column="dept_id"/>
			<one-to-many class="Employee"/>
		</bag>
	</class>
</hibernate-mapping>

 其中:

bag标签要与list集合对应,name属性是一对多的属性,是Department对象中的属性。 

key标签的column属性是Employee对象中的数据库字段值

one-to-many标签是一对多中的多对象

bag必须和list集合对应,跟list标签的区别是bag没有顺序,list有顺序。

四、map

语法:

<map name="" access="" batch-size="" cascade="" catalog="" check=""  
      		 collection-type="" embed-xml="true" fetch="join" inverse="false"  
      		 lazy="true" mutable="true" node="" optimistic-lock="true"    
      		 outer-join="true" persister="" schema="" subselect="" table=""  
      		 where="">
      		 	<cache usage="read-only"/>  
	            <comment></comment>  
	            <composite-element class=""></composite-element>  
	            <composite-index class=""></composite-index>
	            <composite-map-key class=""></composite-map-key>
	            <element></element>  
	            <filter name=""></filter>  
	            <index></index>
	            <index-many-to-any id-type=""></index-many-to-any>
	            <index-many-to-many class=""></index-many-to-many>
	            <key></key>  
	            <loader query-ref=""/>  
	            <many-to-any id-type=""></many-to-any>  
	            <many-to-many></many-to-many>  
	            <map-key></map-key>
	            <map-key-many-to-many></map-key-many-to-many>
	            <meta attribute=""></meta>  
	            <one-to-many/>  
	            <sql-delete></sql-delete>  
	            <sql-delete-all></sql-delete-all>  
	            <sql-insert></sql-insert>  
	            <sql-update></sql-update>  
	            <subselect></subselect>  
	            <synchronize table=""/>    
		</map>

例子:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.cos.entity">
	<class name="Department">		
		<id name="id">
			<generator class="native"/>
		</id>
		<property name="name" unique="true" />
		<map name="emps">
			<key column="dept_id"/>
			<map-key type="string" column="key_"/>
			<one-to-many class="Employee"/>
		</map>
	</class>
</hibernate-mapping>

其中:

map标签说明的是一对多用的是map,name是Department中的属性。

key中的clumn是Employee中的字段

map-key说明此map的key定义。

one-to-many定义了一对多中的多字段。

五、数组 array

六、总结

使用Hibernate的时候,如果用到集合,绝大多数用set(配置简单)

现在Hibernate项目中一般很好使用关联关系和继承关系。

相关推荐