實(shí)體BEAN的七種關(guān)系之---------多對(duì)多單向
Many-to-Many Unidirectional Relationship
多對(duì)多的單向關(guān)系,一般來(lái)說(shuō)只是為了節(jié)省數(shù)據(jù)庫(kù)的空間而已,因?yàn)樗恍枰樵冴P(guān)系的一端就可以了,并且它和一對(duì)多的不同之處就在于,一對(duì)多可以用被控端維護(hù)一個(gè)對(duì)主控端的外鍵就可以搞定,而它不行,必須要有一張中間的表來(lái)進(jìn)行關(guān)系的映射,在某種程度上,它也是挺像一對(duì)多的關(guān)系的.這種關(guān)系在現(xiàn)實(shí)中可以用如下關(guān)系來(lái)說(shuō)明它:
人和項(xiàng)目的關(guān)系,一個(gè)人可以參加很多個(gè)項(xiàng)目,一個(gè)項(xiàng)目也可以讓很多人參加,這就是多對(duì)多的關(guān)系,但是我們?cè)谶@里可以限定一下,也就是可以知道一個(gè)人他參加了哪幾個(gè)項(xiàng)目,但是我們不需要知道一個(gè)項(xiàng)目有多少人參加(如果我們需要知道的話,那就是多對(duì)多的雙向關(guān)系了).
代碼如下:
/*
* 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.JoinTable;
import javax.persistence.ManyToMany;
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;
private List<Flight> flights;
private List<Project> projects;
@ManyToMany
public List<Project> getProjects() {
return projects;
}
public void setProjects(List<Project> projects) {
this.projects = projects;
}
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "PersonANDFlight", joinColumns = {@JoinColumn(name = "personID")}, inverseJoinColumns = {@JoinColumn(name = "flightID")})
public List<Flight> getFlights() {
return flights;
}
public void setFlights(List<Flight> flights) {
this.flights = flights;
}
@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)
@JoinColumn(name = "personID")
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;
}
}
Project代碼如下
/*
* Project.java
*
* Created on 2007-9-27, 9:47:01
*
* 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.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Temporal;
/**
*
* @author hadeslee
*/
@Entity
public class Project implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private String name;
private String description;
private Date fromDate;
private Date toDate;
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Temporal(javax.persistence.TemporalType.DATE)
public Date getFromDate() {
return fromDate;
}
public void setFromDate(Date from) {
this.fromDate = from;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Temporal(javax.persistence.TemporalType.DATE)
public Date getToDate() {
return toDate;
}
public void setToDate(Date to) {
this.toDate = to;
}
public void setId(Long id) {
this.id = id;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
}
從代碼中我們可以看出,我們只在關(guān)系的主控端 Person里面加上了@ManyToMany的注釋,而在Project里面卻沒(méi)有任何其它的注釋,但是由于我們是多對(duì)多的關(guān)系,不是一對(duì)多的關(guān)系,是不能由被維護(hù)端的一個(gè)外鍵指向我們自己的,因?yàn)樗锌赡芤赶蚝芏鄠€(gè)人,所以我們只能用一張中間關(guān)系表的方式來(lái)實(shí)現(xiàn)這種關(guān)系.
七種關(guān)系,到現(xiàn)在已經(jīng)全部講完了.其實(shí)我們可以在腦海里面過(guò)一遍.這七種關(guān)系的特點(diǎn)和它適用的地方,在實(shí)際的工作中,需要的是活學(xué)活用.希望大家都能用好這七種關(guān)系.:)
盡管千里冰封
依然擁有晴空
你我共同品味JAVA的濃香.
posted on 2007-09-29 08:42
千里冰封 閱讀(986)
評(píng)論(0) 編輯 收藏 所屬分類:
JAVAEE