5黑马币
@Entity
@Table(name = "t_person", schema = "jpa")
public class Person {
@Id
@GeneratedValue
@Column(name = "id")
private Integer id;
@Column(name = "name", length = 20)
private String name;
@OneToOne(cascade = { CascadeType.ALL }, optional = false)
@JoinColumn(name = "idCard", unique = true)
private IdCard idCard;
// getter & setter
}
@Entity
@Table(name = "t_idcard", schema = "jpa")
public class IdCard {
@Id
@GeneratedValue
@Column(name = "id")
private Integer id;
@Column(name = "cardNo", unique = true, length = 18)
private String cardNo;
@OneToOne(cascade = { CascadeType.MERGE }, mappedBy = "idCard")
private Person person;
// getter & setter
}
不明白为什么以下代码还能保存成功,那岂不是变成多对一关联映射了吗?
IdCard idCard = new IdCard();
idCard.setCardNo("xxxxxxxx");
Person person = new Person();
person.setName("user_01");
person.setIdCard(idCard);
Person person1 = new Person();
person1.setName("user_02");
person1.setIdCard(idCard);
entityManager.persist(person);
entityManager.persist(person1);
|
|