日期:2014-05-16 浏览次数:20362 次
?双向和单向的区别,其实不在数据库,而在加载方向。如Person和IdCard,单向是Person拥有IdCard的实例可以加载IdCard,而双向不但Person可以加载IdCard,IdCard可以拥有Person实例来加载Person。数据库还是原来的数据库(两个共享主键的表)
以下模拟双向一对一得映射案例
表结构如下:
Person表结构
IdCard表结构
?
IDCard类
package com.ljq.entity; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.OneToOne; import javax.persistence.Table; /** * 身份证 * * @author jiqinlin * */ @Entity @Table(name = "tb_idcard") public class IDCard { @Id @GeneratedValue private Integer id; @Column(nullable = false, length = 18) private String cardno; //unique= true 指明personid列的值不可重复。 //optional = false指明Person不可为空 @OneToOne(cascade = CascadeType.ALL, optional = false) @JoinColumn(name = "personid",referencedColumnName="id", unique = true) private Person person;