一对一双向外键关联
一对一双向外键关联
a) 项目名称:hibernate_0700_one2one_bi_fk
b) Annotation:@One2One(mappedBy)
c) xml:<many-to-one unique> <one-to-one property-ref>
d) 规律:凡是双向关联,必设(最佳实践) mappedBy
注解:
配置 Husband 和 Wife 一对一双向外键关联
Husband:
1 package com.bjsxt.hibernate; 2 3 import javax.persistence.Entity; 4 import javax.persistence.GeneratedValue; 5 import javax.persistence.Id; 6 import javax.persistence.JoinColumn; 7 import javax.persistence.OneToOne; 8 9 @Entity 10 public class Husband { 11 12 private Integer id; 13 14 private String name; 15 16 private Wife wife; 17 18 @Id 19 @GeneratedValue 20 public Integer getId() { 21 return id; 22 } 23 24 public String getName() { 25 return name; 26 } 27 28 @OneToOne//一对一的关系 29 @JoinColumn(name="wifeId")//外键ID 30 public Wife getWife() { 31 return wife; 32 } 33 34 public void setId(Integer id) { 35 this.id = id; 36 } 37 38 public void setName(String name) { 39 this.name = name; 40 } 41 42 public void setWife(Wife wife) { 43 this.wife = wife; 44 } 45 }
Wife
1 package com.bjsxt.hibernate; 2 3 import javax.persistence.Entity; 4 import javax.persistence.GeneratedValue; 5 import javax.persistence.Id; 6 import javax.persistence.JoinColumn; 7 import javax.persistence.OneToOne; 8 9 @Entity 10 public class Wife { 11 private Integer id; 12 13 private String name; 14 15 private Husband husband; 16 17 @Id 18 @GeneratedValue 19 public Integer getId() { 20 return id; 21 } 22 23 public void setId(Integer id) { 24 this.id = id; 25 } 26 27 public String getName() { 28 return name; 29 } 30 31 public void setName(String name) { 32 this.name = name; 33 } 34 35 @OneToOne(mappedBy="wife")//wife 为 husband 的 wife 36 //@JoinColumn(name="husbandId") 37 public Husband getHusband() { 38 return husband; 39 } 40 41 public void setHusband(Husband husband) { 42 this.husband = husband; 43 } 44 }
XML 配置
Student(学生) 和 StudentIdCard(学生证) 为一对一双向外键关联
Student
1 package com.bjsxt.hibernate; 2 3 public class Student { 4 5 private Integer id; 6 7 private String name; 8 9 private Integer age; 10 11 private String sex; 12 13 private boolean good; 14 15 private StudentIdCard studentIdCard; 16 17 public Integer getId() { 18 return id; 19 } 20 21 public void setId(Integer id) { 22 this.id = id; 23 } 24 25 public String getName() { 26 return name; 27 } 28 29 public void setName(String name) { 30 this.name = name; 31 } 32 33 public Integer getAge() { 34 return age; 35 } 36 37 public void setAge(Integer age) { 38 this.age = age; 39 } 40 41 public String getSex() { 42 return sex; 43 } 44 45 public void setSex(String sex) { 46 this.sex = sex; 47 } 48 49 public boolean isGood() { 50 return good; 51 } 52 53 public void setGood(boolean good) { 54 this.good = good; 55 } 56 57 public StudentIdCard getStudentIdCard() { 58 return studentIdCard; 59 } 60 61 public void setStudentIdCard(StudentIdCard studentIdCard) { 62 this.studentIdCard = studentIdCard; 63 } 64 65 }
StudentIdCard
1 package com.bjsxt.hibernate; 2 3 public class StudentIdCard { 4 5 private Integer id; 6 7 private String num; 8 9 private Student student; 10 11 public Integer getId() { 12 return id; 13 } 14 15 public void setId(Integer id) { 16 this.id = id; 17 } 18 19 public String getNum() { 20 return num; 21 } 22 23 public void setNum(String num) { 24 this.num = num; 25 } 26 27 public Student getStudent() { 28 return student; 29 } 30 31 public void setStudent(Student student) { 32 this.student = student; 33 } 34 35 }
StudentIdCard.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 6 <hibernate-mapping package="com.bjsxt.hibernate"> 7 <class name="StudentIdCard" table="studentidcard"> 8 <id name="id" column="id"> 9 <generator class="native"/> 10 </id> 11 <property name="num" column="num"/> 12 <many-to-one name="student" column="studentId" unique="true"></many-to-one> 13 </class> 14 </hibernate-mapping>
Student.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 6 <hibernate-mapping package="com.bjsxt.hibernate"> 7 <class name="Student" table="student" dynamic-update="true"> 8 <id name="id" column="id"> 9 <generator class="native"/> 10 </id> 11 <property name="name" column="name"/> 12 <property name="age" column="age"/> 13 <property name="sex" column="sex"/> 14 <property name="good" type="yes_no"/> 15 <!-- 表示以StudentIdCard 为主导,自己不再产生额外的外键 --> 16 <one-to-one name="studentIdCard" property-ref="student"></one-to-one> 17 </class> 18 </hibernate-mapping>
jar 包链接: https://pan.baidu.com/s/1nvqK2F3 密码: 529r
代码链接: https://pan.baidu.com/s/1dEXxUt7 密码: sxbb