實體BEAN的七種關(guān)系之---------一對一單向
一對一單向,顧名思義,就是只要求從A方到達B方,而不需要從B方到達A方,典型的例子就是,一個人對應(yīng)一個地址,因為現(xiàn)實生活中,一個地址可能住很多人,所以一般我們只需要根據(jù)人查到它的地址,而不太會需要從一個地址去查誰住在那里,不過,真的有這種需求的話,我們就要以用另外一種關(guān)系來實現(xiàn)了,這個以后再講
首先我們聲明一個人的實體類,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.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;
@OneToOne(cascade=CascadeType.ALL,optional=true)
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;
}
}
再來看看Address的實體BEAN定義
/*
* Address.java
*
* Created on 2007-9-15, 0:13:50
*
* 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 Address implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private String country,province,city,street;
private int postcode;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public int getPostcode() {
return postcode;
}
public void setPostcode(int postcode) {
this.postcode = postcode;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public void setId(Long id) {
this.id = id;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
}
以上便是兩個類的定義,從Person里面我們可以看到如下的代碼
@OneToOne(cascade=CascadeType.ALL,optional=true)
public Address getAddress() {
return address;
}
在這里的@OneToOne就是注釋是一對一的關(guān)系,其中cascade是表示級聯(lián)的關(guān)系,是級聯(lián)刪除還是級聯(lián)更新,還是所有,我們這里選擇的是所有,這樣的好處就是,我們在插入Person的時候,就順帶的把Person里面包含的Address一起插入數(shù)據(jù)庫里面,當我們刪除Person的時候,也是把這個Person對應(yīng)的Address從數(shù)據(jù)庫里面刪除,否則就需要我們?nèi)斯さ膭h除兩遍或者插入兩遍等等,optional表示這個成員是不是可選的,我們這里是可選的,也就是說一個人可以沒有地址(比如流浪漢:)).
然后我們發(fā)現(xiàn),在Address里面只有一些簡單的EntityBean的注釋,并沒有表示關(guān)系的注釋,這是因為本例是一對一單向的實現(xiàn),Person里面有 Address,而Address卻什么都不知道,它對應(yīng)到數(shù)據(jù)庫里面只是一張簡單的表而已,Person對應(yīng)到數(shù)據(jù)庫里面就有一個指向Address的外鍵.我們也可以增加注釋指定外鍵的列的名字,如下:
@OneToOne(cascade=CascadeType.ALL,optional=true)
@JoinColumn(name="addressID")
public Address getAddress() {
return address;
}
如果我們不加的話,也是可以通過的,在JBOSS里面,它會自動幫你生成你指向這個類的類名加上下劃線再加上id的列,也就是默認列名是:address_id.
如果是主鍵相關(guān)聯(lián)的話,那么可以運用如下注釋
@OneToOne(cascade={CascadeType.ALL})
@PrimaryKeyJoinColumn
public Address getAddress( ) {
return homeAddress;
}
它表示兩張表的關(guān)聯(lián)是根據(jù)兩張表的主鍵的
以下是一些注釋的定義,我們看一下可以了解一下這些注釋有哪些方法,
public @interface JoinColumn
{
String name( ) default "";
String referencedColumnName( ) default "";
boolean unique( ) default false;
boolean nullable( ) default true;
boolean insertable( ) default true;
boolean updatable( ) default true;
String columnDefinition( ) default "";
String table( ) default "";
}
public @interface OneToOne
{
Class targetEntity( ) default void.class;
CascadeType[] cascade( ) default {};
FetchType
fetch( ) default EAGER;
boolean optional( ) default true;
String mappedBy( ) default "";
}
public @interface PrimaryKeyJoinColumn
{
String name( ) default "";
String referencedColumnName( ) default "";
String columnDefinition( ) default "";
}