JPA入门(三)双向一对一
package org.jzkangta.onetoone;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;/** * 选择了Person是关系维护端,所以Person为负责外键的更新 * @author Administrator * */@Entitypublic class Person {@Id@GeneratedValueprivate int id;@Column(nullable=false)private String name;@OneToOne(cascade=CascadeType.ALL,optional=false)@JoinColumn(name="idCard_id") //关系维护端设置外键private IDCard idCard;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; }public IDCard getIdCard() { return idCard; }public void setIdCard( IDCard idCard ) { this.idCard = idCard; }}package org.jzkangta.onetoone;import javax.persistence.CascadeType;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.FetchType;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.OneToOne;@Entitypublic class IDCard {@Id@GeneratedValueprivate int id;@Column(length=18,nullable=false)private String idNumber;@OneToOne(mappedBy="idCard",cascade={CascadeType.MERGE,CascadeType.PERSIST,CascadeType.REFRESH},optional=false,fetch=FetchType.EAGER)//被维护端,idCard为维护端里的属性,optional=false不允许为nullprivate Person person;public int getId() { return id; }public void setId( int id ) { this.id = id; }public String getIdNumber() { return idNumber; }public void setIdNumber( String idNumber ) { this.idNumber = idNumber; }public Person getPerson() { return person; }public void setPerson( Person person ) { this.person = person; }}