JPA入門(三)双方向一対一
2517 ワード
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
*
*/
@Entity
public class Person {
@Id
@GeneratedValue
private 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;
@Entity
public class IDCard {
@Id
@GeneratedValue
private 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 null
private 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;
}
}