8.hibernate一对一双向外键关联,jpa映射
husband表结构:
wife表结构
package com.bjsxt.hibernate;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.JoinColumn;import javax.persistence.OneToOne;@Entitypublic class Husband {private int id;private String name;private Wife wife;@Id@GeneratedValuepublic int getId() {return id;}public String getName() {return name;}@OneToOne@JoinColumn(name="wifeId")public Wife getWife() {return wife;}public void setId(int id) {this.id = id;}public void setName(String name) {this.name = name;}public void setWife(Wife wife) {this.wife = wife;}}
package com.bjsxt.hibernate;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.OneToOne;@Entitypublic class Wife {private int id;private String name;private Husband husband;@OneToOne(mappedBy="wife")//关联关系由Husband的wife确定public Husband getHusband() {return husband;}public void setHusband(Husband husband) {this.husband = husband;}@Id@GeneratedValuepublic 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;}}