hibernate的OneToOne映射包括外鍵的單/雙向映射,主鍵的單/雙向映射,聯合主鍵映射等。
關於OneToOne的映射,hibernate的annotation的API做了如下描述:
2.2.5.1. 一對一(One-to-one)
使用@OneToOne註解可以建立實體bean之間的一對一的關聯. 一對一關聯有三種情況: 一是關聯的實體都共享同樣的主鍵, 二是其中一個實體通過外鍵關聯到另一個實體的主鍵 (注意要模擬一對一關聯必須在外鍵列上添加唯一約束). 三是通過關聯表來保存兩個實體之間的連接關係 (注意要模擬一對一關聯必須在每一個外鍵上添加唯一約束).
首先,我們通過共享主鍵來進行一對一關聯映射:
@Entity public class Body { @Id public Long getId() { return id; } @OneToOne(cascade = CascadeType.ALL) @PrimaryKeyJoinColumn public Heart getHeart() { return heart; } ... }
@Entity public class Heart { @Id public Long getId() { ...} }
上面的例子通過使用註解@PrimaryKeyJoinColumn定義了一對一關聯.
下面這個例子使用外鍵列進行實體的關聯.
@Entity public class Customer implements Serializable { @OneToOne(cascade = CascadeType.ALL) @JoinColumn(name="passport_fk") public Passport getPassport() { ... } @Entity public class Passport implements Serializable { @OneToOne(mappedBy = "passport") public Customer getOwner() { ... }
上面這個例子中,Customer 通過Customer 表中名爲的passport_fk 外鍵列和 Passport關聯. @JoinColumn註解定義了聯接列(join column). 該註解和@Column註解有點類似, 但是多了一個名爲referencedColumnName的參數. 該參數定義了所關聯目標實體中的聯接列. 注意,當referencedColumnName關聯到非主鍵列的時候, 關聯的目標類必須實現Serializable, 還要注意的是所映射的屬性對應單個列(否則映射無效).
一對一關聯可能是雙向的.在雙向關聯中, 有且僅有一端是作爲主體(owner)端存在的:主體端負責維護聯接列(即更新). 對於不需要維護這種關係的從表則通過mappedBy屬性進行聲明.mappedBy的值指向主體的關聯屬性. 在上面這個例子中,mappedBy的值爲 passport. 最後,不必也不能再在被關聯端(owned side)定義聯接列了,因爲已經在主體端進行了聲明.
如果在主體沒有聲明@JoinColumn,系統自動進行處理: 在主表(owner table)中將創建聯接列, 列名爲:主體的關聯屬性名+下劃線+被關聯端的主鍵列名. 在上面這個例子中是passport_id, 因爲Customer中關聯屬性名爲passport, Passport的主鍵是id.
The third possibility (using an association table) is very exotic.
第三種方式也許是最另類的(通過關聯表).
@Entity public class Customer implements Serializable { @OneToOne(cascade = CascadeType.ALL) @JoinTable(name = "CustomerPassports", joinColumns = @JoinColumn(name="customer_fk"), inverseJoinColumns = @JoinColumn(name="passport_fk") ) public Passport getPassport() { ... } @Entity public class Passport implements Serializable { @OneToOne(mappedBy = "passport") public Customer getOwner() { ... }
Customer通過名爲 CustomerPassports的關聯表和 Passport關聯; 該關聯表擁有名爲passport_fk的外鍵列,該 外鍵指向Passport表,該信息定義爲inverseJoinColumn的屬性值, 而customer_fk外鍵列指向Customer表, 該信息定義爲 joinColumns的屬性值.
這種關聯可能是雙向的.在雙向關聯中, 有且僅有一端是作爲主體端存在的:主體端負責維護聯接列(即更新). 對於不需要維護這種關係的從表則通過mappedBy屬性進行聲明. mappedBy的值指向主體的關聯屬性. 在上面這個例子中,mappedBy的值爲 passport. 最後,不必也不能再在被關聯端(owned side)定義聯接列了,因爲已經在主體端進行了聲明.
你必須明確定義關聯表名和關聯列名.
一.單向關聯,包括外鍵和主鍵
基於外鍵關聯的單向一對一關聯和單向多對一關聯幾乎是一樣的。唯一的不同就是單向一對一關聯中的外鍵字段具有唯一性約束。
<class name="Person"> <id name="id" column="personId"> <generator class="native"/> </id> <many-to-one name="address" column="addressId" unique="true" not-null="true"/> </class> <class name="Address"> <id name="id" column="addressId"> <generator class="native"/> </id> </class>
create table Person ( personId bigint not null primary key, addressId bigint not null unique ) create table Address ( addressId bigint not null primary key )
<class name="Person"> <id name="id" column="personId"> <generator class="native"/> </id> </class> <class name="Address"> <id name="id" column="personId"> <generator class="foreign"> <param name="property">person</param> </generator> </id> <one-to-one name="person" constrained="true"/> </class>
create table Person ( personId bigint not null primary key ) create table Address ( personId bigint not null primary key )二.使用鏈接表的單向關聯
A unidirectional one-to-one association on a join table is possible, but extremely unusual.
<class name="Person"> <id name="id" column="personId"> <generator class="native"/> </id> <join table="PersonAddress" optional="true"> <key column="personId" unique="true"/> <many-to-one name="address" column="addressId" not-null="true" unique="true"/> </join> </class> <class name="Address"> <id name="id" column="addressId"> <generator class="native"/> </id> </class>
create table Person ( personId bigint not null primary key ) create table PersonAddress ( personId bigint not null primary key, addressId bigint not null unique ) create table Address ( addressId bigint not null primary key )三.雙向關聯
A bidirectional one-to-one association on a foreign key is common:
<class name="Person"> <id name="id" column="personId"> <generator class="native"/> </id> <many-to-one name="address" column="addressId" unique="true" not-null="true"/> </class> <class name="Address"> <id name="id" column="addressId"> <generator class="native"/> </id> <one-to-one name="person" property-ref="address"/> </class>
create table Person ( personId bigint not null primary key, addressId bigint not null unique ) create table Address ( addressId bigint not null primary key )
A bidirectional one-to-one association on a primary key uses the special id generator:
<class name="Person"> <id name="id" column="personId"> <generator class="native"/> </id> <one-to-one name="address"/> </class> <class name="Address"> <id name="id" column="personId"> <generator class="foreign"> <param name="property">person</param> </generator> </id> <one-to-one name="person" constrained="true"/> </class>
create table Person ( personId bigint not null primary key ) create table Address ( personId bigint not null primary key )四.使用鏈接表的雙向關聯
A bidirectional one-to-one association on a join table is possible, but extremely unusual.
<class name="Person"> <id name="id" column="personId"> <generator class="native"/> </id> <join table="PersonAddress" optional="true"> <key column="personId" unique="true"/> <many-to-one name="address" column="addressId" not-null="true" unique="true"/> </join> </class> <class name="Address"> <id name="id" column="addressId"> <generator class="native"/> </id> <join table="PersonAddress" optional="true" inverse="true"> <key column="addressId" unique="true"/> <many-to-one name="person" column="personId" not-null="true" unique="true"/> </join> </class>
create table Person ( personId bigint not null primary key ) create table PersonAddress ( personId bigint not null primary key, addressId bigint not null unique ) create table Address ( addressId bigint not null primary key )
1.OneToOne的外鍵單向映射
先看annotation版本
本例使用Husband和Wife爲例,需要在Husband裏面引用Wife,並在getWife上使用@OneToOne@JoinColumn(name="wifeId")
Husband類
package com.baosight.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
@Entity
public class Husband {
private String id;
private String name;
private Wife wife;
@Id
@GeneratedValue//auto
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@OneToOne
@JoinColumn(name="wifeId")
public Wife getWife() {
return wife;
}
public void setWife(Wife wife) {
this.wife = wife;
}
}
Wife類
package com.baosight.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Wife {
private String id;
private String name;
@Id
@GeneratedValue//auto
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
使用JUnit進行測試
OrMappingTest類
package com.baosight.model;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
public class OrMappingTest {
private static SessionFactory sf = null;
@BeforeClass
public static void beforeClass(){
// 讀取配置文件
Configuration cfg = new AnnotationConfiguration();
// 得到session工廠
sf = cfg.configure().buildSessionFactory();
}
@Test
public void testSchemaExport() {
new SchemaExport(new AnnotationConfiguration().configure()).create(false, true);
}
@AfterClass
public static void afterClass(){
// 關閉session工廠
sf.close();
}
}
運行結果爲:
2.OneToOne的外鍵單向映射
看看xml版本
使用Student和StudentCard,.,需要在StudentCard裏面引用Student,並在StudentCard.hbm.xml使用<many-to-one name="student" column="studentId" unique="true"></many-to-one>
Student類package com.baosight.model;
public class Student {
private String id;
private String name;
private int age;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
StudentCard類
package com.baosight.model;
public class StudentCard {
private String id;
private String num;
private Student student;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
}
Student.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.baosight.model">
<class name="Student" dynamic-update="true">
<id name="id" >
<generator class="native"></generator>
</id>
<property name="name"></property>
<property name="age"></property>
</class>
</hibernate-mapping>
StudentCard.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.baosight.model">
<class name="StudentCard" dynamic-update="true">
<id name="id" >
<generator class="native"></generator>
</id>
<property name="num"></property>
<many-to-one name="student" column="studentId" unique="true"></many-to-one>
</class>
</hibernate-mapping>
使用JUnit測試以及結果見1中的描述
3.OneToOne的外鍵雙向映射
看看annotation版本
本例使用Husband和Wife爲例,需要在Husband裏面引用Wife,並在getWife上使用@OneToOne@JoinColumn(name="wifeId"),並在Wife裏面引用Husband,並在getHusband上使用@OneToOne(mappedBy="wife"),需要注意的是,雙向關聯的映射一般需要在其中一個類中使用mappedBy="wife",表示關聯關係由Husband中的wife來維護。
Husband類與1中配置相同,這裏不再贅述。
Wife類
package com.baosight.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
@Entity
public class Wife {
private String id;
private String name;
private Husband husband;
@Id
@GeneratedValue//auto
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@OneToOne(mappedBy="wife")
public Husband getHusband() {
return husband;
}
public void setHusband(Husband husband) {
this.husband = husband;
}
}
使用JUnit測試,與1中配置相同,不再贅述。測試結果如下:
4.OneToOne的外鍵雙向映射
看看xml版本
使用Student和StudentCard,.,需要在StudentCard裏面引用Student,並在StudentCard.hbm.xml使用<many-to-one name="student" column="studentId" unique="true"></many-to-one>,另外需要在Student裏面引用StudentCard,並在Student.hbm.xml使用<one-to-one name="studentCard" property-ref="student"></one-to-one>,其中property-ref="student"表示關聯關係由StudentCard的student來維持,同樣地,在雙向關聯中這種配置是比較常見的。
StudentCard和StudentCard.hbm.xml的配置與2中完全相同,這裏不再贅述。
Student類
package com.baosight.model;
public class Student {
private String id;
private String name;
private int age;
private StudentCard studentCard;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public StudentCard getStudentCard() {
return studentCard;
}
public void setStudentCard(StudentCard studentCard) {
this.studentCard = studentCard;
}
}
Student.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.baosight.model">
<class name="Student" dynamic-update="true">
<id name="id" >
<generator class="native"></generator>
</id>
<property name="name"></property>
<property name="age"></property>
<one-to-one name="studentCard" property-ref="student"></one-to-one>
</class>
</hibernate-mapping>
仍然使用JUnit進行測試(詳見1),結果爲:
5.OneToOne的主鍵單向映射
先看annotation版本
本例使用Husband和Wife爲例,需要在Husband裏面引用Wife,並在getWife上使用@OneToOne@PrimaryKeyJoinColumn
Husband類
package com.baosight.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
@Entity
public class Husband {
private String id;
private String name;
private Wife wife;
@Id
@GeneratedValue//auto
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@OneToOne
@PrimaryKeyJoinColumn
public Wife getWife() {
return wife;
}
public void setWife(Wife wife) {
this.wife = wife;
}
}
Wife類
package com.baosight.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Wife {
private String id;
private String name;
@Id
@GeneratedValue//auto
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
仍然使用1中的JUnit進行測試,結果爲:
6.OneToOne的主鍵單向映射
看看xml版本
使用Student和StudentCard,.,需要在StudentCard裏面引用Student,並在StudentCard.hbm.xml使用<id name="id" >
<generator class="foreign">
<param name="property">student</param>
</generator>
</id>
和<one-to-one name="student" constrained="true"></one-to-one>,表示主鍵生成參考Student類色主鍵
StudentCard、Student和Student.hbm.xml的配置與2中完全相同,不再贅述
StudentCard.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.baosight.model">
<class name="StudentCard" dynamic-update="true">
<id name="id" >
<generator class="foreign">
<param name="property">student</param>
</generator>
</id>
<property name="num"></property>
<one-to-one name="student" constrained="true"></one-to-one>
</class>
</hibernate-mapping>
測試方法與5中相同,不再贅述,測試結果見5中
7.OneToOne的主鍵雙向映射
看看annotation版本
本例使用Husband和Wife爲例,需要在Husband裏面引用Wife,並在getWife上使用@OneToOne@PrimaryKeyJoinColumn,並在Wife裏面引用Husband,並在getHusband上使用@OneToOne@PrimaryKeyJoinColumn。
Husband類
package com.baosight.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
@Entity
public class Husband {
private String id;
private String name;
private Wife wife;
@Id
@GeneratedValue//auto
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@OneToOne
@PrimaryKeyJoinColumn
public Wife getWife() {
return wife;
}
public void setWife(Wife wife) {
this.wife = wife;
}
}
Wife類
package com.baosight.model;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.SequenceGenerator;
import javax.persistence.TableGenerator;
@Entity
public class Wife {
private String id;
private String name;
private Husband husband;
@Id
@GeneratedValue//auto
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@OneToOne
@PrimaryKeyJoinColumn
public Husband getHusband() {
return husband;
}
public void setHusband(Husband husband) {
this.husband = husband;
}
}
JUnit測試同上,結果爲:
8.OneToOne的主鍵雙向映射
看看xml版本
使用Student和StudentCard,.,需要在StudentCard裏面引用Student,並在StudentCard.hbm.xml使用
<generator class="foreign">
<param name="property">student</param>
</generator>
</id>
和<one-to-one name="student" constrained="true"></one-to-one>,表示主鍵生成參考Student類色主鍵
,另外需要在Student裏面引用StudentCard,並在Student.hbm.xml使用<one-to-one name="studentCard" property-ref="student"></one-to-one>,其中property-ref="student"表示關聯關係由StudentCard的student來維持,同樣地,在雙向關聯中這種配置是比較常見的。StudentCard和StudentCard.hbm.xml的配置與6中完全相同,這裏不再贅述。
Student類
package com.baosight.model;
public class Student {
private String id;
private String name;
private int age;
private StudentCard studentCard;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public StudentCard getStudentCard() {
return studentCard;
}
public void setStudentCard(StudentCard studentCard) {
this.studentCard = studentCard;
}
}
Student.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.baosight.model">
<class name="Student" dynamic-update="true">
<id name="id" >
<generator class="native"></generator>
</id>
<property name="name"></property>
<property name="age"></property>
<one-to-one name="studentCard" property-ref="student"></one-to-one>
</class>
</hibernate-mapping>
測試結果見7中
9.OneToOne的使用聯合主鍵的類的外鍵映射
看看annotation版本
本例使用Husband和Wife爲例,在Wife使用聯合主鍵,創建聯合住建磊WifePK,其需要implements Serializable和重寫equals和hashCode方法,Wife中使用@IdClass(WifePK.class)和@Id。需要在Husband裏面引用Wife,並在getWife上使用@OneToOne
@JoinColumns(
{
@JoinColumn(name="wifeId",referencedColumnName="id"),
@JoinColumn(name="wifeName",referencedColumnName="name")
}
)
WifePK類
package com.baosight.model;
import java.io.Serializable;
import javax.persistence.Embeddable;
/**
* 聯合主鍵類
* <p>Title:WifePK </p>
* <p>Description:TODO </p>
* <p>Company: </p>
* @author yuan
* @date 2016-4-19 下午8:25:56
*/
//@Embeddable
public class WifePK implements Serializable{
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object obj) {
// TODO Auto-generated method stub
if(obj instanceof StudentPK){
StudentPK pk = (StudentPK)obj;
if(this.id.equals(pk.getId())&&this.name.equals(pk.getName())){
return true;
}
}
return false;
}
@Override
public int hashCode() {
// TODO Auto-generated method stub
return this.id.hashCode();
}
}
Wife類
package com.baosight.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.IdClass;
@Entity
@IdClass(WifePK.class)
public class Wife {
private String id;
private String name;
private String age;
@Id
@GeneratedValue//auto
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Id
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
測試類同上,結果爲:
10.OneToOne的使用聯合主鍵的類的外鍵映射
看看xml版本
使用Student和StudentCard,.,在Student使用聯合主鍵,創建聯合住建磊StudentPK,其需要implements Serializable和重寫equals和hashCode方法,需要在Student中引用StudentPK,並在Student.hbm.xml中使用<composite-id name="pk" class="StudentPK">
<key-property name="id" column="stu_id"></key-property>
<key-property name="name" column="stu_name"></key-property>
</composite-id>,不要再使用id了,需要注意。
需要在StudentCard裏面引用Student,並在StudentCard.hbm.xml使用
<many-to-one name="student" class="Student" insert="false" update="false">
<column name="stu_id"></column>
<column name="stu_name"></column>
</many-to-one>
StudentPK
package com.baosight.model;
import java.io.Serializable;
/**
* <p>Title:StudentPK </p>
* <p>Description:TODO </p>
* <p>Company: </p>
* @author yuan
* @date 2016-4-15 下午8:08:16*/
public class StudentPK implements Serializable{
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object obj) {
// TODO Auto-generated method stub
if(obj instanceof StudentPK){
StudentPK pk = (StudentPK)obj;
if(this.id.equals(pk.getId())&&this.name.equals(pk.getName())){
return true;
}
}
return false;
}
@Override
public int hashCode() {
// TODO Auto-generated method stub
return this.id.hashCode();
}
}
Student
package com.baosight.model;
import javax.persistence.Column;
import javax.persistence.OneToOne;
public class Student {
// private String id;
// private String name;
private int age;
private StudentPK pk;
/* public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}*/
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public StudentPK getPk() {
return pk;
}
public void setPk(StudentPK pk) {
this.pk = pk;
}
}
StudentCard
package com.baosight.model;
public class StudentCard {
private String id;
private String num;
private Student student;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
}
Student.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.baosight.model">
<class name="Student" dynamic-update="true">
<!-- <id name="id" >
<generator class="native"></generator>
</id> -->
<composite-id name="pk" class="StudentPK">
<key-property name="id" column="stu_id"></key-property>
<key-property name="name" column="stu_name"></key-property>
</composite-id>
<!-- <property name="name"></property> -->
<property name="age"></property>
</class>
</hibernate-mapping>
StudentCard.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.baosight.model">
<class name="StudentCard" dynamic-update="true">
<id name="id" >
<generator class="native"></generator>
</id>
<property name="num"></property>
<many-to-one name="student" class="Student" insert="false" update="false">
<column name="stu_id"></column>
<column name="stu_name"></column>
</many-to-one>
</class>
</hibernate-mapping>
使用上面的JUnit進行測試,結果爲:
以上即爲OneToOne映射的內容,其實在實際使用中OneToOne用的不多,不過學習OneToOne對於學習後面的ManyToOne和ManyToMany有幫助,可以看作是它們的特殊情形。總之,上述各種映射關係需要在實際的使用中仔細體會。