一对一单向双向主键关联

一对一单向主键关联(不重要)
a) @PrimaryKey JoinColumn

  注解实现 Husband 和 Wife--以Husband为主为例

    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.OneToOne;
 7 import javax.persistence.PrimaryKeyJoinColumn;
 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     @PrimaryKeyJoinColumn
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 
31 }
View Code

XML 实现

    以 Student 和 StudentIdCard(学生证) 为例,Student 为主导

    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 }

Student.hbm.xml 设置主键生成策略为 foreign,参数为 studentIdCard,constrained="true"

 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="foreign">
10                 <param name="property">studentIdCard</param>
11             </generator>
12         </id>
13         <property name="name" column="name"/>
14         <property name="age" column="age"/>
15         <property name="sex" column="sex"/>
16         <property name="good" type="yes_no"/>
17         <one-to-one name="studentIdCard" constrained="true"></one-to-one>
18     </class>
19 </hibernate-mapping>

StudentIdCard

 1 package com.bjsxt.hibernate;
 2 
 3 public class StudentIdCard {
 4 
 5     private Integer id;
 6     
 7     private String num;
 8 
 9     public Integer getId() {
10         return id;
11     }
12 
13     public void setId(Integer id) {
14         this.id = id;
15     }
16 
17     public String getNum() {
18         return num;
19     }
20 
21     public void setNum(String num) {
22         this.num = num;
23     }
24     
25 }

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     </class>
13 </hibernate-mapping>
View Code

所需jar包链接: https://pan.baidu.com/s/1bo7J9mB 密码: p1qh

代码链接: https://pan.baidu.com/s/1c1DNg6c 密码: mhrf


4. 一对一双向主键关联(不重要)

   Annotation 配置

    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.OneToOne;
 7 import javax.persistence.PrimaryKeyJoinColumn;
 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     @PrimaryKeyJoinColumn
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 }
View Code

    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.OneToOne;
 7 import javax.persistence.PrimaryKeyJoinColumn;
 8 
 9 @Entity
10 public class Wife {
11     
12     private Integer id;
13     
14     private String name;
15     
16     private Husband husband;
17     
18     @Id
19     @GeneratedValue
20     public Integer getId() {
21         return id;
22     }
23 
24     public void setId(Integer id) {
25         this.id = id;
26     }
27 
28     public String getName() {
29         return name;
30     }
31 
32     public void setName(String name) {
33         this.name = name;
34     }
35 
36     @OneToOne
37     @PrimaryKeyJoinColumn
38     public Husband getHusband() {
39         return husband;
40     }
41 
42     public void setHusband(Husband husband) {
43         this.husband = husband;
44     }
45 
46 }
View Code

如上图所示,并没有生成对应的外键。Annotation 在这个版本有bug,别用。

XML 配置:

  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 }
View Code

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">
 8         <id name="id" column="id">
 9             <generator class="foreign">
10                 <param name="property">studentIdCard</param>
11             </generator>
12         </id>
13         <property name="name" column="name"/>
14         <property name="age" column="age"/>
15         <property name="sex" column="sex"/>
16         <property name="good" type="yes_no"/>
17         <one-to-one name="studentIdCard" constrained="true"></one-to-one>
18     </class>
19 </hibernate-mapping>

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.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         <one-to-one name="student" property-ref="studentIdCard"></one-to-one>
13     </class>
14 </hibernate-mapping>

所需jar包链接: https://pan.baidu.com/s/1bo7J9mB 密码: p1qh

代码链接: https://pan.baidu.com/s/1pLx1WiZ 密码: i8d5

posted @ 2017-04-19 11:31  流年如水烟雨随风  阅读(967)  评论(0编辑  收藏  举报