實(shí)體BEAN的七種關(guān)系之---------一對(duì)多雙向
One-to-Many Bidirectional Relationship
在實(shí)際生活中,一對(duì)多的雙向關(guān)系也是有的,我們?cè)谶@里舉一個(gè)人和車的例子,人可以有很多車,車也必須要有一個(gè)主人(只要它是合法的),我們可以知道一個(gè)人有多少輛車,我們也可以通過(guò)任意一輛車牌號(hào)查到這輛車的主人是誰(shuí),這種關(guān)系不像人和電話,電話是很容易換的,并且很多號(hào)碼是不用身份證的,但是車必須要上牌并且要用身份證的,這樣才好管理嘛.下面我們來(lái)看代碼吧
還是一樣,先定義一個(gè)Person類(我們的Person已經(jīng)一天一天的變大了:)
/*
* 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.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
/**
*
* @author Admin
*/
@Entity
public class Person implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private String name;
private String sex;
private int age;
private Address address;
private List<Phone> phones;
private IDCard idCard;
private Country country;
private List<Car> cars;
@OneToMany(cascade=CascadeType.ALL,mappedBy="person")
public List<Car> getCars() {
return cars;
}
public void setCars(List<Car> cars) {
this.cars = cars;
}
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="countryID")
public Country getCountry() {
return country;
}
public void setCountry(Country country) {
this.country = country;
}
@OneToOne(cascade=CascadeType.ALL)
public IDCard getIdCard() {
return idCard;
}
public void setIdCard(IDCard idCard) {
this.idCard = idCard;
}
@OneToMany(cascade=CascadeType.ALL)
public List<Phone> getPhones() {
return phones;
}
public void setPhones(List<Phone> phones) {
this.phones = phones;
}
@OneToOne(cascade={CascadeType.ALL})
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;
}
}
然后我們?cè)俣x一個(gè)Car類
/*
* Car.java
*
* Created on 2007-9-20, 19:40:39
*
* 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.Date;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Temporal;
/**
*
* @author hadeslee
*/
@Entity
public class Car implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private Person person;
private String carNumber;
private String carName;
private Date date;
public String getCarName() {
return carName;
}
public void setCarName(String carName) {
this.carName = carName;
}
public String getCarNumber() {
return carNumber;
}
public void setCarNumber(String carNumber) {
this.carNumber = carNumber;
}
@Temporal(javax.persistence.TemporalType.DATE)
public Date getCarDate() {
return date;
}
public void setCarDate(Date date) {
this.date = date;
}
@ManyToOne(cascade=CascadeType.ALL,optional=false)
@JoinColumn(name="personID")
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ì)多其實(shí)和雙向的一對(duì)一差不多,一個(gè)是關(guān)系的主控端,一個(gè)是關(guān)系的被維護(hù)端.
,在一對(duì)一的雙向關(guān)系里面,我們可以讓任意一方做關(guān)系的主控端,任意一方做關(guān)系的被維護(hù)端(mapped 來(lái)注釋),但是一對(duì)多或者說(shuō)多一對(duì)的雙向關(guān)系中,主控端必須是多的那一方,也就是Car,在它里面起一個(gè)外鍵指向Person類的主鍵,然后我們?cè)趯?duì)它們進(jìn)行處理的時(shí)候,必須兩端都要設(shè)置一下,才能保證數(shù)據(jù)的更新是如我們所愿的,比如
Person p=new Person();
Car car=new Car();
....
p.setCar(car);
car.setPerson(p);
entityManager.persist(p);
這個(gè)時(shí)候會(huì)把car和p都寫入數(shù)據(jù)庫(kù),當(dāng)我們要把car的主人換成p1時(shí),需要做如下動(dòng)作
p.getCars().remove(car);
car.setPerson(p1);
p1.setCar(car);
這樣就可以了,其實(shí)只要我們保持一個(gè)良好的習(xí)慣,那就是更新關(guān)系的時(shí)候,雙方都更新一下,這樣就不容易出錯(cuò)了.
盡管千里冰封
依然擁有晴空
你我共同品味JAVA的濃香.
posted on 2007-09-22 09:20
千里冰封 閱讀(1119)
評(píng)論(1) 編輯 收藏 所屬分類:
JAVAEE