Hibernate的1-1映射关系主要分为两类:
1.Hibernate基于外键映射的1对1关联关系
对于基于外键的1-1关联,其外键可以存放在任意一边,在需要存放外键一端,增加many-to-one元素。为many-to-one元素增加unique="true"属性表示为1-1关联
1 <many-to-one name="mgr" class="com.yl.hibernate.one2one.foreign.Manager" column="MGR_ID" unique="true"></many-to-one>
另一端需要使用one-to-one元素,该元素使用property-ref属性指定使用被关联实体主键以外的字段作为关联字段
1 <one-to-one name="dept" class="com.yl.hibernate.one2one.foreign.Department" property-ref="mgr"></one-to-one>
--如果不使用property-ref属性,SQL如下:
1 from 2 MANAGERS manager0_ 3 left outer join 4 DEPARTMENTS department1_ 5 on manager0_.MGR_ID=department1_.DEPT_ID
--如果使用property-ref属性,SQL如下:
1 from 2 MANAGERS manager0_ 3 left outer join 4 DEPARTMENTS department1_ 5 on manager0_.MGR_ID=department1_.MGR_ID
------------------------------------代码------------------------------------------------------------------
Department.hbm.xml
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 <!-- Generated 2014-11-29 20:05:53 by Hibernate Tools 3.4.0.CR1 --> 5 <hibernate-mapping> 6 <class name="com.yl.hibernate.one2one.foreign.Department" table="DEPARTMENTS"> 7 <id name="deptId" type="java.lang.Integer"> 8 <column name="DEPT_ID" /> 9 <generator class="native" /> 10 </id> 11 <property name="deptName" type="java.lang.String"> 12 <column name="DEPT_NAME" /> 13 </property> 14 15 <!-- 使用many-to-one 的方式来映射1-1关联关系 --> 16 <many-to-one name="mgr" class="com.yl.hibernate.one2one.foreign.Manager" column="MGR_ID" unique="true"></many-to-one> 17 18 </class> 19 </hibernate-mapping>
Manager.hbm.xml
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 <!-- Generated 2014-11-29 20:05:53 by Hibernate Tools 3.4.0.CR1 --> 5 <hibernate-mapping> 6 <class name="com.yl.hibernate.one2one.foreign.Manager" table="MANAGERS"> 7 <id name="mgrId" type="java.lang.Integer"> 8 <column name="MGR_ID" /> 9 <generator class="native" /> 10 </id> 11 <property name="mgrName" type="java.lang.String"> 12 <column name="MGR_NAME" /> 13 </property> 14 15 <!-- 映射1-1的关联关系:在对应的数据表中已经有外键了,当前持久化类使用one-to-one 进行映射 --> 16 <!-- 没有外键的一端需要使用one-to-one元素,该元素使用property-ref 属性指定使用被关联实体主键以外的字段作为关联字段 --> 17 <one-to-one name="dept" class="com.yl.hibernate.one2one.foreign.Department" property-ref="mgr"></one-to-one> 18 19 20 21 </class> 22 </hibernate-mapping>
2.Hibernate基于主键映射的1对1关联关系
基于主键的映射策略:指一端的主键生成器使用foreign策略,表明根据“”对方“的主键来生成自己的主键,自己并不能独立生成主键。<param>子元素指定使用当前持久化类的哪个属性作为"对方"
1 <id name="deptId" type="java.lang.Integer"> 2 <column name="DEPT_ID" /> 3 <!-- 使用外键的方式来生成当前的主键 --> 4 <generator class="foreign"> 5 <!-- property属性指定使用当前持久化类的哪一个属性的主键作为外键 --> 6 <param name="property">mgr</param> 7 </generator> 8 </id>
采用foreign主键生成器策略的一端增加one-to-one元素映射关联属性,其one-to-one属性还应增加constrained="true"属性;另一端增加one-to-one元素映射关联属性
constrained(约束):指定为当前持久化类对应的数据库表的主键添加一个外键约束,引用被关联的对象("对方")所对应的数据库表主键
------------------------------------代码------------------------------------------------------------------
Department.hbm.xml
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 <!-- Generated 2014-11-29 20:05:53 by Hibernate Tools 3.4.0.CR1 --> 5 <hibernate-mapping> 6 <class name="com.yl.hibernate.one2one.primary.Department" table="DEPARTMENTS"> 7 <id name="deptId" type="java.lang.Integer"> 8 <column name="DEPT_ID" /> 9 <!-- 使用外键的方式来生成当前的主键 --> 10 <generator class="foreign"> 11 <!-- property属性指定使用当前持久化类的哪一个属性的主键作为外键 --> 12 <param name="property">mgr</param> 13 </generator> 14 </id> 15 <property name="deptName" type="java.lang.String"> 16 <column name="DEPT_NAME" /> 17 </property> 18 19 <one-to-one name="mgr" class="com.yl.hibernate.one2one.primary.Manager" constrained="true"></one-to-one> 20 </class> 21 </hibernate-mapping>
Manager.hbm.xml
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 <!-- Generated 2014-11-29 20:05:53 by Hibernate Tools 3.4.0.CR1 --> 5 <hibernate-mapping package="com.yl.hibernate.one2one.primary"> 6 <class name="Manager" table="MANAGERS"> 7 <id name="mgrId" type="java.lang.Integer"> 8 <column name="MGR_ID" /> 9 <generator class="native" /> 10 </id> 11 <property name="mgrName" type="java.lang.String"> 12 <column name="MGR_NAME" /> 13 </property> 14 15 <!-- 16 采用foreign主键生成器策略的一端增加one-to-one元素映射关联属性, 17 其one-to-one节点还应增加constrained="true"属性;以使当前的主键上添加外键约束 18 --> 19 <one-to-one name="dept" class="Department" property-ref="mgr"></one-to-one> 20 21 22 23 </class> 24 </hibernate-mapping>