千里冰封
JAVA 浓香四溢
posts - 151,comments - 2801,trackbacks - 0
 实体BEAN的七种关系之---------多对一单向
Many-to-One Unidirectional Relationship

多对一单向在某种程度上不但和一对一单向相似并且还和一对多单向挺相似的,但是又不完全相同。多一对单向一般应用在很多实体对应一个实体,被对应的那个实体并不需要知道谁对应它了,典型的例子就是人对应国家,很多人可以是同一个国家的人,但是一个国家却不可能统计那么多它的人民。我们还是用代码来说话吧。

先定义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类和以前一些实体类是一样的,只有最基本的主键注释,对于有人和他关联的事情,他是一点都不知道的。我们可以在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这个属性了,因为在一对多一对多一对的关系里面,关系的维护端都是在多的那一面,所以ManyToOne是没有mapped这个属性的,当然OneToMany也可以不用这个属性而使当前表占主导地位,就像昨天的那个例子一样。因为主控端是Person .外键也是建在Person上面,因为它是多的一面。当然我们在这里也可以省掉@JoinColumn,那样的话会怎么样呢,会不会像一对多单向一样生成中间的表呢?事实是不会的,在这里如果我们去掉@JoinColumn的话,那么一样会在Person表里面生成一列指向Country的外键,这一点和一对多的单向是不一样,在一对多的单向里面,如果我们不在Person 里面加上@JoinColumn这个注释,那么JBOSS将会为我们生成一个中间的表,这个表会有一个列指向Person主键,一个列指向Phone主键。所以说为了程序有一定的行为,有些东西我们还是不要省的好。

其实多对一单向是有点向一对一单向的,在主控端里面,也就是从Person的角度来看,也就是对应了一个Country而已,只不过这个Country是很多Person所共用的,而一对一却没有这一点限制。


尽管千里冰封
依然拥有晴空

你我共同品味JAVA的浓香.
posted on 2007-09-21 09:14 千里冰封 阅读(830) 评论(0)  编辑  收藏 所属分类: JAVAEE

只有注册用户登录后才能发表评论。


网站导航: