人员表:一个人只能对应一个身份证
@Entity
@Table(name="bb_person")
public class Person implements Serializable
{
/**/
private static final long serialVersionUID = 1L;
private int personId;
private String personName;
private IdCard idcard = new IdCard();
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="person_id")
public int getPersonId()
{
return personId;
}
public void setPersonId(int personId)
{
this.personId = personId;
}
public String getPersonName()
{
return personName;
}
public void setPersonName(String personName)
{
this.personName = personName;
}
@OneToOne(cascade=CascadeType.ALL)
@PrimaryKeyJoinColumn
public IdCard getIdcard()
{
return idcard;
}
public void setIdcard(IdCard idcard)
{
this.idcard = idcard;
}
}
身份证表:一个身份证只能对应一个人
@Entity
@Table(name="bb_idcard")
public class IdCard
{
private int cardId;
private String cardNo;
private Person person;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
public int getCardId()
{
return cardId;
}
public void setCardId(int cardId)
{
this.cardId = cardId;
}
public String getCardNo()
{
return cardNo;
}
public void setCardNo(String cardNo)
{
this.cardNo = cardNo;
}
@OneToOne(mappedBy="idcard")
public Person getPerson()
{
return person;
}
public void setPerson(Person person)
{
this.person = person;
}
}
测试方法:
public class TestPerson
{
public PersonDao getPersonDao()
{
ApplicationContext ac = new ClassPathXmlApplicationContext("spring_lushuifa.xml");
return (PersonDao) ac.getBean("personDao");
}
@Test
public void addPersonAndIdCard()
{
Person person = new Person();
person.setPersonName("张三");
person.getIdcard().setCardNo("3602222222222222");
System.out.println("测试对象是否有用:"+person);
this.getPersonDao().addPerson(person);
}
}