博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
一对一双向外键关联
阅读量:4983 次
发布时间:2019-06-12

本文共 4556 字,大约阅读时间需要 15 分钟。

一对一双向外键关联

  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 @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 import javax.persistence.JoinColumn; 7 import javax.persistence.OneToOne; 8  9 @Entity10 public class Wife {11     private Integer id;12     13     private String name;14     15     private Husband husband;16 17     @Id18     @GeneratedValue19     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 的 wife36     //@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 }
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.hbm.xml

1 
2 5 6
7
8
9
10
11
12
13
14

 

Student.hbm.xml

1 
2 5 6
7
8
9
10
11
12
13
14
15
16
17
18

jar 包链接: https://pan.baidu.com/s/1nvqK2F3 密码: 529r

代码链接: https://pan.baidu.com/s/1dEXxUt7 密码: sxbb

转载于:https://www.cnblogs.com/ShawnYang/p/6730504.html

你可能感兴趣的文章
yii2 实战教程之如何安装
查看>>
Spring整合Hibernate与Struts
查看>>
ORA-02041: client database did not begin a transaction
查看>>
判断浏览器JS
查看>>
U33405 纽约
查看>>
method_missing in JavaScript
查看>>
Leetcode 904. Fruit Into Baskets
查看>>
第三百九十天 how can I 坚持
查看>>
NGUI插件的一个扩展---NGUI_HUD_Text
查看>>
urllib2模块使用
查看>>
怎样连接REDIS服务端
查看>>
餐饮平台报表图片
查看>>
Windows server 2008搭建php运行环境
查看>>
scala基础
查看>>
【转载】前后端分离的思考与实践(三)
查看>>
浅析rune数据类型
查看>>
76.数塔问题
查看>>
2016.10.30 NOIP模拟赛 day2 AM 整理
查看>>
算法设计10-红黑搜索树
查看>>
CentOS 离线安装 MYSQL+APACHE+PHP
查看>>