husband表结构:
[img]http://dl.iteye.com/upload/attachment/592490/07008a28-3ac9-30e7-a3e0-93d38a55e599.jpg[/img]
wife表结构
[img]http://dl.iteye.com/upload/attachment/592488/a0274cbe-2211-3562-b052-4047a1213f43.jpg[/img]
[img]http://dl.iteye.com/upload/attachment/592490/07008a28-3ac9-30e7-a3e0-93d38a55e599.jpg[/img]
wife表结构
[img]http://dl.iteye.com/upload/attachment/592488/a0274cbe-2211-3562-b052-4047a1213f43.jpg[/img]
package com.bjsxt.hibernate;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
@Entity
public class Husband {
private int id;
private String name;
private Wife wife;
@Id
@GeneratedValue
public 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;
@Entity
public 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
@GeneratedValue
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;
}
}