http://blog.csdn.net/princetoad/archive/2007/10/10/1817693.aspx
一对一单向,顾名思义,就是只要求从A方到达B方,而不需要从B方到达A方,典型的例子就是,一个人对应一个地址,因为现实生活中,一个地址可能住很多 人,所以一般我们只需要根据人查到它的地址,而不太会需要从一个地址去查谁住在那里,不过,真的有这种需求的话,我们就要以用另外一种关系来实现了,这个 以后再讲
首先我们声明一个人的实体类,Person

 /**//*
/**//*
 * Person.java
 * Person.java
 *
 * 
 * Created on 2007-9-15, 0:11:58
 * Created on 2007-9-15, 0:11:58
 *
 * 
 * To change this template, choose Tools | Templates
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 * and open the template in the editor.
 */
 */

 package lbf.entitybean.test1;
package lbf.entitybean.test1;

 import java.io.Serializable;
import java.io.Serializable;
 import java.util.List;
import java.util.List;
 import javax.persistence.CascadeType;
import javax.persistence.CascadeType;
 import javax.persistence.Entity;
import javax.persistence.Entity;
 import javax.persistence.GeneratedValue;
import javax.persistence.GeneratedValue;
 import javax.persistence.GenerationType;
import javax.persistence.GenerationType;
 import javax.persistence.Id;
import javax.persistence.Id;
 import javax.persistence.OneToMany;
import javax.persistence.OneToMany;
 import javax.persistence.OneToOne;
import javax.persistence.OneToOne;


 /** *//**
/** *//**
 *
 *
 * @author Admin
 * @author Admin
 */
 */
 @Entity
@Entity

 public class Person implements Serializable
public class Person implements Serializable  {
{
 private static final long serialVersionUID = 1L;
    private static final long serialVersionUID = 1L;
 private Long id;
    private Long id;
 private String name;
    private String name;
 private String sex;
    private String sex;
 private int age;
    private int age;
 private Address address;
    private Address address;
 
    
 @OneToOne(cascade=CascadeType.ALL,optional=true)
    @OneToOne(cascade=CascadeType.ALL,optional=true)

 public Address getAddress()
    public Address getAddress()  {
{
 return address;
        return address;
 }
    }


 public void setAddress(Address address)
    public void setAddress(Address address)  {
{
 this.address = address;
        this.address = address;
 }
    }

 public int getAge()
    public int getAge()  {
{
 return age;
        return age;
 }
    }


 public void setAge(int age)
    public void setAge(int age)  {
{
 this.age = age;
        this.age = age;
 }
    }


 public String getName()
    public String getName()  {
{
 return name;
        return name;
 }
    }


 public void setName(String name)
    public void setName(String name)  {
{
 this.name = name;
        this.name = name;
 }
    }


 public String getSex()
    public String getSex()  {
{
 return sex;
        return sex;
 }
    }


 public void setSex(String sex)
    public void setSex(String sex)  {
{
 this.sex = sex;
        this.sex = sex;
 }
    }

 public void setId(Long id)
    public void setId(Long id)  {
{
 this.id = id;
        this.id = id;
 }
    }

 @Id
    @Id
 @GeneratedValue(strategy = GenerationType.AUTO)
    @GeneratedValue(strategy = GenerationType.AUTO)

 public Long getId()
    public Long getId()  {
{
 return id;
        return id;
 }
    }

 }
}

再来看看Address的实体BEAN定义

 /**//*
/**//*
 * Address.java
 * Address.java
 *
 * 
 * Created on 2007-9-15, 0:13:50
 * Created on 2007-9-15, 0:13:50
 *
 * 
 * To change this template, choose Tools | Templates
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 * and open the template in the editor.
 */
 */

 package lbf.entitybean.test1;
package lbf.entitybean.test1;

 import java.io.Serializable;
import java.io.Serializable;
 import javax.persistence.Entity;
import javax.persistence.Entity;
 import javax.persistence.GeneratedValue;
import javax.persistence.GeneratedValue;
 import javax.persistence.GenerationType;
import javax.persistence.GenerationType;
 import javax.persistence.Id;
import javax.persistence.Id;


 /** *//**
/** *//**
 *
 *
 * @author Admin
 * @author Admin
 */
 */
 @Entity
@Entity

 public class Address implements Serializable
public class Address implements Serializable  {
{
 private static final long serialVersionUID = 1L;
    private static final long serialVersionUID = 1L;
 private Long id;
    private Long id;
 private String country,province,city,street;
    private String country,province,city,street;
 private int postcode;
    private int postcode;

 public String getCity()
    public String getCity()  {
{
 return city;
        return city;
 }
    }


 public void setCity(String city)
    public void setCity(String city)  {
{
 this.city = city;
        this.city = city;
 }
    }


 public String getCountry()
    public String getCountry()  {
{
 return country;
        return country;
 }
    }


 public void setCountry(String country)
    public void setCountry(String country)  {
{
 this.country = country;
        this.country = country;
 }
    }


 public int getPostcode()
    public int getPostcode()  {
{
 return postcode;
        return postcode;
 }
    }


 public void setPostcode(int postcode)
    public void setPostcode(int postcode)  {
{
 this.postcode = postcode;
        this.postcode = postcode;
 }
    }


 public String getStreet()
    public String getStreet()  {
{
 return street;
        return street;
 }
    }


 public void setStreet(String street)
    public void setStreet(String street)  {
{
 this.street = street;
        this.street = street;
 }
    }

 public void setId(Long id)
    public void setId(Long id)  {
{
 this.id = id;
        this.id = id;
 }
    }

 @Id
    @Id
 @GeneratedValue(strategy = GenerationType.AUTO)
    @GeneratedValue(strategy = GenerationType.AUTO)

 public Long getId()
    public Long getId()  {
{
 return id;
        return id;
 }
    }

 }
}

以上便是两个类的定义,从Person里面我们可以看到如下的代码
@OneToOne(cascade=CascadeType.ALL,optional=true)
    public Address getAddress() {
        return address;
    }
在这里的@OneToOne就是注释是一对一的关系,其中cascade是表示级联的关系,是级联删除还是级联更新,还是所有,我们这里选择的是所有,这 样的好处就是,我们在插入Person的时候,就顺带的把Person里面包含的Address一起插入数据库里面,当我们删除Person的时候,也是 把这个Person对应的Address从数据库里面删除,否则就需要我们人工的删除两遍或者插入两遍等等,optional表示这个成员是不是可选的, 我们这里是可选的,也就是说一个人可以没有地址(比如流浪汉:)).
然后我们发现,在Address里面只有一些简单的EntityBean的注释,并没有表示关系的注释,这是因为本例是一对一单向的实现,Person里 面有 Address,而Address却什么都不知道,它对应到数据库里面只是一张简单的表而已,Person对应到数据库里面就有一个指向 Address的外键.我们也可以增加注释指定外键的列的名字,如下:
@OneToOne(cascade=CascadeType.ALL,optional=true)
@JoinColumn(name="addressID")
    public Address getAddress() {
        return address;
    }
如果我们不加的话,也是可以通过的,在JBOSS里面,它会自动帮你生成你指向这个类的类名加上下划线再加上id的列,也就是默认列名是:address_id.
如果是主键相关联的话,那么可以运用如下注释
@OneToOne(cascade={CascadeType.ALL})
   @PrimaryKeyJoinColumn
   public Address getAddress( ) {
      return homeAddress;
   }
它表示两张表的关联是根据两张表的主键的
以下是一些注释的定义,我们看一下可以了解一下这些注释有哪些方法,
 public @interface JoinColumn
public @interface JoinColumn


 {
{
 String name( ) default "";
   String name( ) default "";
 String referencedColumnName( ) default "";
   String referencedColumnName( ) default "";
 boolean unique( ) default false;
   boolean unique( ) default false;
 boolean nullable( ) default true;
   boolean nullable( ) default true;
 boolean insertable( ) default true;
   boolean insertable( ) default true;
 boolean updatable( ) default true;
   boolean updatable( ) default true;
 String columnDefinition( ) default "";
   String columnDefinition( ) default "";
 String table( ) default "";
   String table( ) default "";
 }
}

 public @interface OneToOne
public @interface OneToOne


 {
{
 Class targetEntity( ) default void.class;
   Class targetEntity( ) default void.class;

 CascadeType[] cascade( ) default
   CascadeType[] cascade( ) default  {};
{};
 FetchType
   FetchType 
 fetch( ) default EAGER;
 fetch( ) default EAGER;
 boolean optional( ) default true;
   boolean optional( ) default true;
 String mappedBy( ) default "";
   String mappedBy( ) default "";
 }
}

 public @interface PrimaryKeyJoinColumn
public @interface PrimaryKeyJoinColumn


 {
{
 String name( ) default "";
   String name( ) default "";
 String referencedColumnName( ) default "";
   String referencedColumnName( ) default "";
 String columnDefinition( ) default "";
   String columnDefinition( ) default "";
 }
}

好了,一对一单向差不多就这么些吧,明天再看一下一对一双向的情况.