一対一の関係

4457 ワード


 
@Entity

@Table(name="person_one")

public class Person {

 private Integer id;

 private String name;

 private IDCard idcard;

 

 @OneToOne(cascade={CascadeType.ALL},optional=false)

 @JoinColumn(name="idcard_id")

 public IDCard getIdcard() {

  return idcard;

 }



 public void setIdcard(IDCard idcard) {

  this.idcard = idcard;

 }



 @Id @GeneratedValue

 public Integer getId() {

  return id;

 }

 

 public void setId(Integer id) {

  this.id = id;

 }

 @Column(length=12,nullable=false)

 public String getName() {

  return name;

 }

 public void setName(String name) {

  this.name = name;

 }

 



}



 





@Entity

@Table(name="idcard_one")

public class IDCard {

 private Integer id;

 private String cardno;

 private Person person;

 

 @OneToOne(cascade={CascadeType.PERSIST,CascadeType.MERGE,CascadeType.REFRESH}

 ,mappedBy="idcard")

 public Person getPerson() {

  return person;

 }

 public void setPerson(Person person) {

  this.person = person;

 }

 @Id @GeneratedValue

 public Integer getId() {

  return id;

 }

 public void setId(Integer id) {

  this.id = id;

 }

 

 @Column(length=18,nullable=false)

 public String getCardno() {

  return cardno;

 }

 public void setCardno(String cardno) {

  this.cardno = cardno;

 }

 



}