實體BEAN的七種關系之---------一對多雙向
One-to-Many Bidirectional Relationship
在實際生活中,一對多的雙向關系也是有的,我們在這里舉一個人和車的例子,人可以有很多車,車也必須要有一個主人(只要它是合法的),我們可以知道一個人有多少輛車,我們也可以通過任意一輛車牌號查到這輛車的主人是誰,這種關系不像人和電話,電話是很容易換的,并且很多號碼是不用身份證的,但是車必須要上牌并且要用身份證的,這樣才好管理嘛.下面我們來看代碼吧
還是一樣,先定義一個Person類(我們的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.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;
}
}
然后我們再定義一個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;
}
}
從這里我們可以看到雙向的一對多其實和雙向的一對一差不多,一個是關系的主控端,一個是關系的被維護端.
,在一對一的雙向關系里面,我們可以讓任意一方做關系的主控端,任意一方做關系的被維護端(mapped 來注釋),但是一對多或者說多一對的雙向關系中,主控端必須是多的那一方,也就是Car,在它里面起一個外鍵指向Person類的主鍵,然后我們在對它們進行處理的時候,必須兩端都要設置一下,才能保證數據的更新是如我們所愿的,比如
Person p=new Person();
Car car=new Car();
....
p.setCar(car);
car.setPerson(p);
entityManager.persist(p);
這個時候會把car和p都寫入數據庫,當我們要把car的主人換成p1時,需要做如下動作
p.getCars().remove(car);
car.setPerson(p1);
p1.setCar(car);
這樣就可以了,其實只要我們保持一個良好的習慣,那就是更新關系的時候,雙方都更新一下,這樣就不容易出錯了.
盡管千里冰封
依然擁有晴空
你我共同品味JAVA的濃香.
posted on 2007-09-22 09:20
千里冰封 閱讀(1114)
評論(1) 編輯 收藏 所屬分類:
JAVAEE