如果下面部分内容有不明白的可以查找:
基于Annotation的关系映射 前期准备:http://blog.csdn.net/p_3er/article/details/9061911
基于映射文件共享主键方式实现一对一:http://blog.csdn.net/p_3er/article/details/9004419
基于映射文件唯一外键式实现一对一:http://blog.csdn.net/p_3er/article/details/9004471
1、共享主键方式:
Person:
@Entity
@Table(name = "person", catalog = "hibernate")
public class Person implements java.io.Serializable {
private Integer id;
private String name;
private IdCard idCard;
public Person() {
}
@Id
@GeneratedValue
@Column(name = "id", unique = true, nullable = false)
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name = "name", nullable = false, length = 45)
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@OneToOne(cascade = CascadeType.ALL)
@PrimaryKeyJoinColumn // 这个注解只能写在主(生成ID)的一端,指向从表
public IdCard getIdCard() {
return idCard;
}
public void setIdCard(IdCard idCard) {
this.idCard = idCard;
}
}
IdCard:
@Entity
@Table(name = "id_card", catalog = "hibernate")
public class IdCard implements java.io.Serializable {
private Integer id;
private Person person;
private String cardNo;
public IdCard() {
}
@Id
@GenericGenerator(name = "abc", strategy = "foreign", parameters = { @Parameter(name = "property", value = "person") })
@GeneratedValue(generator = "abc")
@Column(name = "id", unique = true, nullable = false)
/*
与映射文件相比较:
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="foreign">
<param name="property">person</param>
</generator>
</id>
*/
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "id", unique = true, nullable = false, insertable = false, updatable = false)
public Person getPerson() {
return this.person;
}
public void setPerson(Person person) {
this.person = person;
}
@Column(name = "card_no", nullable = false, length = 45)
public String getCardNo() {
return this.cardNo;
}
public void setCardNo(String cardNo) {
this.cardNo = cardNo;
}
}
测试与使用和基于映射文件基本一样:http://blog.csdn.net/p_3er/article/details/9004419
2、唯一外键方式
Person:
@Entity
@Table(name = "person", catalog = "hibernate")
public class Person implements java.io.Serializable {
private Integer id;
private String name;
private IdCard idCard;
public Person() {
}
@Id
@GeneratedValue
@Column(name = "id", unique = true, nullable = false)
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name = "name", nullable = false, length = 45)
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@OneToOne(cascade = CascadeType.ALL,mappedBy="person")
//<one-to-one name="idCard" property-ref="person"/> 与之前的映射文件比较
//当指定person中的mappedBy后,关系只能被idcard来主动维护
public IdCard getIdCard() {
return idCard;
}
public void setIdCard(IdCard idCard) {
this.idCard = idCard;
}
}
IdCard:
@Entity
@Table(name = "id_card", catalog = "hibernate")
public class IdCard implements java.io.Serializable {
private Integer id;
private Person person;
private String cardNo;
public IdCard() {
}
@Id
@GeneratedValue
@Column(name = "id", unique = true, nullable = false)
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "person_id", unique = true)
/*
比较:
<many-to-one name="person" column="person_id" unique="true" />
*/
public Person getPerson() {
return this.person;
}
public void setPerson(Person person) {
this.person = person;
}
@Column(name = "card_no", nullable = false, length = 45)
public String getCardNo() {
return this.cardNo;
}
public void setCardNo(String cardNo) {
this.cardNo = cardNo;
}
}