1、一對一相對比較簡單。hibernate主要是幫助我們保持了兩張表的id一致!
先建兩張表
- create table h_01.idcard (id integer not null, num integer, primary key (id));
- create table h_01.student (id integer not null, name varchar(255), primary key (id));
然後是POJO
- package modle;
-
- public class IdCard {
- private int id;
- private int num;
- private Student student;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public int getNum() {
- return num;
- }
- public void setNum(int num) {
- this.num = num;
- }
- public Student getStudent() {
- return student;
- }
- public void setStudent(Student student) {
- this.student = student;
- }
- }
- package modle;
-
- public class Student {
- private int id;
- private String name;
- private IdCard idCard;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public IdCard getIdCard() {
- return idCard;
- }
- public void setIdCard(IdCard idCard) {
- this.idCard = idCard;
- }
-
- }