EJB学习日记(14)

本文解析了实体Bean中的多对多单向关系概念及其在数据库中的实现方式,通过具体示例说明了如何在Java中使用注解定义这种关系。
2007年10月10日 07:00:00
实体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里面却没有任何其它的注释,但是由于我们是多对多的关系,不是一对多的关系,是 不能由被维护端的一个外键指向我们自己的,因为它有可能要指向很多个人,所以我们只能用一张中间关系表的方式来实现这种关系.

七种关系,到现在已经全部讲完了.其实我们可以在脑海里面过一遍.这七种关系的特点和它适用的地方,在实际的工作中,需要的是活学活用.希望大家都能用好这七种关系.:)


来自: 千里冰封

Trackback: http://tb.blog.youkuaiyun.com/TrackBack.aspx?PostId=1817687


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值