一对一单向外键关联

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 @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 
 7 @Entity
 8 public class Wife {
 9     private Integer id;
10     
11     private String name;
12 
13     @Id
14     @GeneratedValue
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 }

加上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

posted @ 2017-04-18 17:43  流年如水烟雨随风  阅读(412)  评论(0编辑  收藏  举报