实体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);
这样就可以了,其实只要我们保持一个良好的习惯,那就是更新关系的时候,双方都更新一下,这样就不容易出错了.