1、hibernate一對一主鍵關聯映射(單向關聯Person----->IdCard)node
一對一關聯映射原理:讓兩個實體的主鍵同樣,這樣就不須要加入多餘的字段了app
Person.hbm.xml.net
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>
IdCard.hbm.xmlhibernate
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;code
//省略set()、get()方法xml
}對象
public class IdCard {
private int id;
private String cardNo;get
//省略set()、get()方法generator
}io
2、hibernate一對一主鍵關聯映射(雙向關聯Person<----->IdCard)
須要在IdCard加入<one-to-one>標籤,指示hibernate將關聯對象Person 根據主鍵加載上來
<one-to-one>不影響存儲,隻影響加載
Person.hbm.xml
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>
IdCard.hbm.xml
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()方法
}
3、hibernate一對一惟一外鍵關聯映射(單向關聯Person----->IdCard)
一對一惟一外鍵關聯映射實際上是多對一的特例
採用<many-to-one>標籤來映射,指定多的一端unique爲true,這樣就限制了多的一端的多重性 爲一,就是這樣來映射的。
Person.hbm.xml
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>
IdCard.hbm.xml
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()方法
}
4、hibernate一對一惟一外鍵關聯映射(雙向關聯Person<----->IdCard)
一對一惟一外鍵關聯雙向採用<one-to-one>標籤映射,必須指定<one-to-one> 標籤中的property-ref屬性爲關係字段的名稱
Person.hbm.xml
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>
IdCard.hbm.xml
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()方法
}