关于关系映射的一些基本知识点,请查看总结篇:Hibernate 关系映射
Annotation方式:
对象模型:一Husband 对一 wife ,且此时Husband为主导方
在主导方使用 @OneToOne@JoinColumn, 被主导方使用@OneToOne(mappedBy)
与单向一对一关联不同的是:双向关联在被主导方加上了@OneToOne注解。
mappedBy属性的作用是告诉Husband类,Wife类中的属性已经映射过了,不必在此处映射,以免产生冗余
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 int id;
private String name;
private Wife wife;
@Id
@GeneratedValue
public int getId() { return id; }
public String getName() { return name; }
@OneToOne
@JoinColumn(name="wifeId")
public Wife getWife() { return wife; }
public void setId(int id) { this.id = id; }
public void setName(String name) { this.name = name; }
public void setWife(Wife wife) { this.wife = wife; }
}
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Wife {
private int id;
private String name;
private Husband husband;
@Id
@GeneratedValue
public int getId() { return id; }
public String getName() { return name; }
public void setId(int id) { this.id = id; }
public void setName(String name) { this.name = name; }
@OneToOne(mappedBy="wife")
public Husband getHusband() { return husband; }
public void setHusband(Husband husband) { this.husband= husband; }
}
xml方式:
对象模型:学生(主导方)和学生证一对一关系
为实现映射,需要在Student.hbm.xm(主导方)中增加
<one-to-one name="studentCard"property-ref="student"></one-to-one> (Property-ref相当于mappedBy)
在StudentCard.hbm.xml(被主导方)中增加
<many-to-onename="student" column="id"unique="true"></many-to-one>
column属性是指数据库表中外键名称
unique="true"是保证生成的字段唯一,这样<many-to-one>也达到了一对一的效果
public class Student {
private int id;
private int age;
private String name;
private StudentCard studentCard;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setStudentCard(StudentCard studentCard) {
this.studentCard = studentCard;
}
public StudentCard getStudentCard() {
return studentCard;
}
}
public class StudentCard {
private int id;
private String name;
private Student student;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
}
StudentCard.hbm.xml:
<hibernate-mapping>
<class name="com.zyh.hibernate.StudentCard" table="studentcard">
<id name="id">
<generator class="native"></generator>
</id>
<property name="name"></property>
<many-to-one name="student" column="id" unique="true"></many-to-one>
</class>
</hibernate-mapping>
Student.hbm.xml
<hibernate-mapping>
<class name="com.zyh.hibernate.Student" table="student" dynamic-update="true">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="name" column="name" />
<property name="age" column="age" />
<one-to-one name="studentCard" property-ref="student"></one-to-one>
</class>
</hibernate-mapping>
特别说明:
一对一单向外键关联与一对一双向外键关联在数据库的表的格式是一样的区别在于程序中双向外键关联可通过Hibernate在两个类间互相调用彼此而单向外键关联只能单方向调用.