Posted on 2007-10-12 10:45
semovy 閱讀(247)
評論(0) 編輯 收藏 所屬分類:
EJB
實體BEAN的七種關系之---------多對多單向
Many-to-Many Unidirectional Relationship
多對多的單向關系,一般來說只是為了節省數據庫的空間而已,因為它只需要查詢關系的一端就可以了,并且它和一對多的不同之處就在于,一對多可以用被控端維護一個對主控端的外鍵就可以搞定,而它不行,必須要有一張中間的表來進行關系的映射,在某種程度上,它也是挺像一對多的關系的.這種關系在現實中可以用如下關系來說明它:
人和項目的關系,一個人可以參加很多個項目,一個項目也可以讓很多人參加,這就是多對多的關系,但是我們在這里可以限定一下,也就是可以知道一個人他參加了哪幾個項目,但是我們不需要知道一個項目有多少人參加(如果我們需要知道的話,那就是多對多的雙向關系了).
代碼如下:
/*
* 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;
}
}
從代碼中我們可以看出,我們只在關系的主控端 Person里面加上了@ManyToMany的注釋,而在Project里面卻沒有任何其它的注釋,但是由于我們是多對多的關系,不是一對多的關系,是不能由被維護端的一個外鍵指向我們自己的,因為它有可能要指向很多個人,所以我們只能用一張中間關系表的方式來實現這種關系.
七種關系,到現在已經全部講完了.其實我們可以在腦海里面過一遍.這七種關系的特點和它適用的地方,在實際的工作中,需要的是活學活用.希望大家都能用好這七種關系.:)