實(shí)體BEAN的七種關(guān)系之---------一對(duì)一雙向
One-to-One Bidirectional Relationship
一對(duì)一雙向的情況其實(shí)在現(xiàn)實(shí)生活中也是挺常見的,最常見的就是人和身份證的關(guān)系了,我們每個(gè)人都有一個(gè)身份證(只要你年滿16歲,并且申請(qǐng)了),并且每個(gè)身份證都對(duì)應(yīng)一個(gè)人,我們可以根據(jù)一個(gè)人查到他的身份證號(hào)碼,也可以根據(jù)一個(gè)身份證號(hào)碼查到一個(gè)人的具體情況,(第一代身份證重碼的情況還是有的:( ) .好了,閑話不多說,還是看看代碼吧.
先定義一個(gè)Person類
/*
* Person.java
*
* Created on 2007-9-15, 0:11:58
*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package lbf.entitybean.test1;
import java.io.Serializable;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
/**
*
* @author Admin
*/
@Entity
public class Person implements Serializable {
private IDCard idCard;
private static final long serialVersionUID = 1L;
private Long id;
private String name;
private String sex;
private int age;
private Address address;
@OneToOne(cascade=CascadeType.ALL,optional=true)
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public void setId(Long id) {
this.id = id;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
@OneToOne(cascade={CascadeType.ALL},optional=true)
public IDCard getIdCard() {
return idCard;
}
public void setIdCard(IDCard iDCard) {
this.idCard = iDCard;
}
}
* Person.java
*
* Created on 2007-9-15, 0:11:58
*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package lbf.entitybean.test1;
import java.io.Serializable;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
/**
*
* @author Admin
*/
@Entity
public class Person implements Serializable {
private IDCard idCard;
private static final long serialVersionUID = 1L;
private Long id;
private String name;
private String sex;
private int age;
private Address address;
@OneToOne(cascade=CascadeType.ALL,optional=true)
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public void setId(Long id) {
this.id = id;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
@OneToOne(cascade={CascadeType.ALL},optional=true)
public IDCard getIdCard() {
return idCard;
}
public void setIdCard(IDCard iDCard) {
this.idCard = iDCard;
}
}
IDCard類
/*
* IDCard.java
*
* Created on 2007-9-18, 17:05:51
*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package lbf.entitybean.test1;
import java.io.Serializable;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
/**
*
* @author hadeslee
*/
@Entity
public class IDCard implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private Person person;
private String cardNumber;
public String getCardNumber() {
return cardNumber;
}
public void setCardNumber(String cardNumber) {
this.cardNumber = cardNumber;
}
@OneToOne(cascade=CascadeType.ALL,mappedBy="idCard",optional=false)
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public void setId(Long id) {
this.id = id;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
}
* IDCard.java
*
* Created on 2007-9-18, 17:05:51
*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package lbf.entitybean.test1;
import java.io.Serializable;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
/**
*
* @author hadeslee
*/
@Entity
public class IDCard implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private Person person;
private String cardNumber;
public String getCardNumber() {
return cardNumber;
}
public void setCardNumber(String cardNumber) {
this.cardNumber = cardNumber;
}
@OneToOne(cascade=CascadeType.ALL,mappedBy="idCard",optional=false)
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public void setId(Long id) {
this.id = id;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
}
在這里我們可以看到一對(duì)一單向和一對(duì)一雙向其實(shí)差別也不是很大,無非就是在對(duì)方的類里面也定義了一個(gè)自己,這樣就互相都能找到對(duì)方了,在數(shù)據(jù)表的表現(xiàn)來看,和一對(duì)一單向是一模一樣的,都是從Person表里面起一個(gè)外鍵連到IDCard的主鍵.但是只有這種方法嗎?
在Person類里面,定義的IDCard注釋是和Address一樣的,可是我們?cè)贗DCard里面定義的注釋卻和Person里面的不一樣:
@OneToOne(cascade=CascadeType.ALL,mappedBy="idCard",optional=false)
public Person getPerson() {
return person;
}
多了一個(gè)mappedBy這個(gè)方法,它表示什么呢?它表示此表和Person的關(guān)系是定義在Person里面的idCard這個(gè)成員上面的,它表示此表是一對(duì)一關(guān)系中的從表,也就是關(guān)系是在person表里面維護(hù)的,這一點(diǎn)很重要.
我們也可以讓主導(dǎo)權(quán)在IDCard上面,也就是讓它產(chǎn)生一個(gè)指向Person的外鍵,這也是可以的,但是最好是讓 Person來維護(hù)整個(gè)關(guān)系,這樣更符合我們正常的思維.
我們也可以看到在Person里面IDCard是注釋是optional=true,也就是說一個(gè)人是可以沒有身份證的,但是一個(gè)身份證不可以沒有人,所以在IDCard里面注釋person的時(shí)候,optional就為false了,這樣就可以防止一個(gè)空的身份證記錄進(jìn)數(shù)據(jù)庫.