6.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;@Entitypublic class Wife {private int id;private String name;@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;}}