hibernate一对一关联映射的四种方式
一、hibernate一对一主键关联映射(单向关联Person----->IdCard)
一对一关联映射原理:让两个实体的主键一样,这样就不需要加入多余的字段了
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <class name="com.bjpowernode.hibernate.Person" table="t_person"> 2 3 <id name="id"> 4 5 <!-- 采用foreign生成策略,forgeign会取得关联对象的标识 --> 6 7 <generator class="foreign"> 8 9 <!-- property只关联对象 --> 10 11 <param name="property">idCard</param> 12 13 </generator> 14 15 </id> 16 17 <property name="name"/> 18 19 <!-- one-to-one指示hibernate如何加载其关联对象,默认根据主键加载 也就是拿到关系字段值,根据对端的主键来加载关联对象 constrained="true表示,当前主键(person的主键)还是一个外键 参照了对端的主键(IdCard的主键),也就是会生成外键约束语句 --> 20 21 <one-to-one name="idCard" constrained="true"/> 22 23 </class>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <class name="gdou.wteam.hibernate.IdCard" table="t_idCard"> 2 <id name="id"> 3 <generator class="native"/> 4 </id> 5 <property name="cardNo"/> 6 </class>
public class Person {
private int id;
private String name;
private IdCard idCard;
//省略set()、get()方法
}
public class IdCard {
private int id;
private String cardNo;
//省略set()、get()方法
}
二、hibernate一对一主键关联映射(双向关联Person<----->IdCard)
需要在IdCard加入<one-to-one>标签,指示hibernate将关联对象Person 根据主键加载上来
<one-to-one>不影响存储,只影响加载
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 5 <hibernate-mapping> 6 <class name="gdou.wteam.hibernate.Person" table="t_person"> 7 <id name="id"> 8 <!-- 采用foreign生成策略,forgeign会取得关联对象的标识 --> 9 <generator class="foreign"> 10 <!-- property只关联对象 --> 11 <param name="property">idCard</param> 12 </generator> 13 </id> 14 <property name="name"/> 15 <!-- 16 one-to-one指示hibernate如何加载其关联对象,默认根据主键加载 17 也就是拿到关系字段值,根据对端的主键来加载关联对象 18 19 constrained="true表示,当前主键(person的主键)还是一个外键 20 参照了对端的主键(IdCard的主键),也就是会生成外键约束语句 21 --> 22 <one-to-one name="idCard" constrained="true"/> 23 </class> 24 </hibernate-mapping>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 5 <hibernate-mapping> 6 <class name="gdou.wteam.hibernate.IdCard" table="t_idCard"> 7 <id name="id"> 8 <generator class="native"/> 9 </id> 10 <property name="cardNo"/> 11 <one-to-one name="person" /> 12 </class> 13 </hibernate-mapping>
public class Person {
private int id;
private String name;
private IdCard idCard;
//省略set()、get()方法
}
public class IdCard {
private int id;
private String cardNo;
private Person person;
//省略set()、get()方法
}
三、hibernate一对一唯一外键关联映射(单向关联Person----->IdCard)
一对一唯一外键关联映射其实是多对一的特例
采用<many-to-one>标签来映射,指定多的一端unique为true,这样就限制了多的一端的多重性 为一,就是这样来映射的。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 5 <hibernate-mapping> 6 <class name="gdou.wteam.hibernate.Person" table="t_person"> 7 <id name="id"> 8 <generator class="native"/> 9 </id> 10 <property name="name"/> 11 <many-to-one name="idCard" unique="true"/> 12 </class> 13 </hibernate-mapping>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 5 <hibernate-mapping> 6 <class name="gdou.wteam.hibernate.IdCard" table="t_idCard"> 7 <id name="id"> 8 <generator class="native"/> 9 </id> 10 <property name="cardNo"/> 11 </class> 12 </hibernate-mapping>
public class Person {
private int id;
private String name;
private IdCard idCard;
//省略set()、get()方法
}
public class IdCard {
private int id;
private String cardNo;
//省略set()、get()方法
}
四、hibernate一对一唯一外键关联映射(双向关联Person<----->IdCard)
一对一唯一外键关联双向采用<one-to-one>标签映射,必须指定<one-to-one> 标签中的property-ref属性为关系字段的名称
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 5 <hibernate-mapping> 6 <class name="gdou.wteam.hibernate.Person" table="t_person"> 7 <id name="id"> 8 <generator class="native"/> 9 </id> 10 <property name="name"/> 11 <many-to-one name="idCard" unique="true"/> 12 </class> 13 </hibernate-mapping>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 5 <hibernate-mapping> 6 <class name="gdou.wteam.hibernate.IdCard" table="t_idCard"> 7 <id name="id"> 8 <generator class="native"/> 9 </id> 10 <property name="cardNo"/> 11 <one-to-one name="person" property-ref="idCard"></one-to-one> 12 </class> 13 </hibernate-mapping>
public class Person {
private int id;
private String name;
private IdCard idCard;
//省略set()、get()方法
}
public class IdCard {
private int id;
private String cardNo;
private Person person;
//省略set()、get()方法
}