一對一單向外部索引鍵關聯,一對一單向鍵關聯
Annotation:@One2One @JoinColumn
xml:<many-to-one unique>
一般情況大致如中所設計,在一個表中設計另一個表的外鍵;
就是在 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 @Entity10 public class Husband {11 12 private Integer id;13 14 private String name;15 16 private Wife wife;17 18 @Id19 @GeneratedValue20 public Integer getId() {21 return id;22 }23 24 public String getName() {25 return name;26 }27 28 @OneToOne//一對一的關係29 @JoinColumn(name="wifeId")//外鍵ID30 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 7 @Entity 8 public class Wife { 9 private Integer id;10 11 private String name;12 13 @Id14 @GeneratedValue15 public Integer getId() {16 return id;17 }18 19 public void setId(Integer id) {20 this.id = id;21 }22 23 public String getName() {24 return name;25 }26 27 public void setName(String name) {28 this.name = name;29 }30 }
加上hibernate.cfg.xml配置:
1 <mapping class="com.bjsxt.hibernate.Husband"/>2 <mapping class="com.bjsxt.hibernate.Wife"/>
SchemaExport代碼:
@Test public void testSchemaExport(){ new SchemaExport(new AnnotationConfiguration().configure()).create(false, true); }
如,資料庫會自動產生兩個表並且建立 husband 表到 wife 表的外部索引鍵關聯
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 public Integer getId() {16 return id;17 }18 19 public void setId(Integer id) {20 this.id = id;21 }22 23 public String getName() {24 return name;25 }26 27 public void setName(String name) {28 this.name = name;29 }30 31 public Integer getAge() {32 return age;33 }34 35 public void setAge(Integer age) {36 this.age = age;37 }38 39 public String getSex() {40 return sex;41 }42 43 public void setSex(String sex) {44 this.sex = sex;45 }46 47 public boolean isGood() {48 return good;49 }50 51 public void setGood(boolean good) {52 this.good = good;53 }54 55 }View Code
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 }View Code
在 StudentIdCard 中加了 student 對象,在StudentIdCard.hbm.xml中的配置,進行一對一的單向關聯:
1 <many-to-one name="student" column="studentId" unique="true"></many-to-one>
所需jar包連結: https://pan.baidu.com/s/1i5fxsjF 密碼: qw9u
代碼連結: https://pan.baidu.com/s/1dFzin3z 密碼: b7e2