一、一对一单/双向外键关联(Annotation)
Annotation: @OneToOne @JoinColumn(name="")
XML:<many-to-one unique(单向)
<many-to-one unique <one-to-one property-ref(双向)
[b]a)Husband.java[/b]
[b]b)Wife.java[/b]
[color=red][b]注:凡是双向关联,必设:mappedBy[/b][/color]
二、一对一单/双向主键关联(Annotation) (不重要)有bug
Annotation: @OneToOne @PrimaryKeyJoinColumn
a)Husband.java Wife.java的设置一样
Annotation: @OneToOne @JoinColumn(name="")
XML:<many-to-one unique(单向)
<many-to-one unique <one-to-one property-ref(双向)
[b]a)Husband.java[/b]
@Entity
public class Husband {
private int id;
private Wife wief
.........
@Id
@GeneratedValue
public int getId() {
return id;
}
@OneToOne
@JoinColumn(name="wifeId")
public Wife getWife() {
return wife;
}
..........
}
[b]b)Wife.java[/b]
@Entity
public class Wife {
private int id;
……………
@Id
@GeneratedValue public int getId() { return id;
}
//双向关联时加入:
// private Husband husband;
// @OneToOne(mappedBy="wife")
// public Husband getHusband(){
return husband;;
}
………………….
}
[color=red][b]注:凡是双向关联,必设:mappedBy[/b][/color]
二、一对一单/双向主键关联(Annotation) (不重要)有bug
Annotation: @OneToOne @PrimaryKeyJoinColumn
a)Husband.java Wife.java的设置一样
..............
@OneToOne
@PrimaryKeyJoinColumn
public Wife getWife() {
return wife;
}
...........