實體BEAN的七種關(guān)系之---------多對一單向
Many-to-One Unidirectional Relationship
多對一單向在某種程度上不但和一對一單向相似并且還和一對多單向挺相似的,但是又不完全相同。多一對單向一般應(yīng)用在很多實體對應(yīng)一個實體,被對應(yīng)的那個實體并不需要知道誰對應(yīng)它了,典型的例子就是人對應(yīng)國家,很多人可以是同一個國家的人,但是一個國家卻不可能統(tǒng)計那么多它的人民。我們還是用代碼來說話吧。
先定義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;
@ManyToOne
@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;
}
}
再定義Country實體類
/*
* Country.java
*
* Created on 2007-9-20, 0:32:41
*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package lbf.entitybean.test1;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
/**
*
* @author Admin
*/
@Entity
public class Country implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private String name;
private String phoneHead;
private long population;
private String area;
private String useLanguage;
public String getUseLanguage() {
return useLanguage;
}
public void setUseLanguage(String language) {
this.useLanguage = language;
}
public String getArea() {
return area;
}
public void setArea(String area) {
this.area = area;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhoneHead() {
return phoneHead;
}
public void setPhoneHead(String phoneHead) {
this.phoneHead = phoneHead;
}
public long getPopulation() {
return population;
}
public void setPopulation(long population) {
this.population = population;
}
public void setId(Long id) {
this.id = id;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
}
在這里我們可以看到,Country類和以前一些實體類是一樣的,只有最基本的主鍵注釋,對于有人和他關(guān)聯(lián)的事情,他是一點都不知道的。我們可以在Person類看到如下的注釋:
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="countryID")
public Country getCountry() {
return country;
}
@ManyToOne的定義如下
public @interface ManyToOne
{
Class targetEntity( ) default void.class;
CascadeType[] cascade( ) default {};
FetchType fetch( ) default EAGER;
boolean optional( ) default true;
}
在這里就沒有mapped這個屬性了,因為在一對多一對多一對的關(guān)系里面,關(guān)系的維護(hù)端都是在多的那一面,所以ManyToOne是沒有mapped這個屬性的,當(dāng)然OneToMany也可以不用這個屬性而使當(dāng)前表占主導(dǎo)地位,就像昨天的那個例子一樣。因為主控端是Person .外鍵也是建在Person上面,因為它是多的一面。當(dāng)然我們在這里也可以省掉@JoinColumn,那樣的話會怎么樣呢,會不會像一對多單向一樣生成中間的表呢?事實是不會的,在這里如果我們?nèi)サ?#64;JoinColumn的話,那么一樣會在Person表里面生成一列指向Country的外鍵,這一點和一對多的單向是不一樣,在一對多的單向里面,如果我們不在Person 里面加上@JoinColumn這個注釋,那么JBOSS將會為我們生成一個中間的表,這個表會有一個列指向Person主鍵,一個列指向Phone主鍵。所以說為了程序有一定的行為,有些東西我們還是不要省的好。
其實多對一單向是有點向一對一單向的,在主控端里面,也就是從Person的角度來看,也就是對應(yīng)了一個Country而已,只不過這個Country是很多Person所共用的,而一對一卻沒有這一點限制。
盡管千里冰封
依然擁有晴空
你我共同品味JAVA的濃香.
posted on 2007-09-21 09:14
千里冰封 閱讀(845)
評論(0) 編輯 收藏 所屬分類:
JAVAEE