gdufo

           

          實(shí)體BEAN的七種關(guān)系之---------一對一單向

          http://blog.csdn.net/princetoad/archive/2007/10/10/1817693.aspx
          一對一單向,顧名思義,就是只要求從A方到達(dá)B方,而不需要從B方到達(dá)A方,典型的例子就是,一個人對應(yīng)一個地址,因?yàn)楝F(xiàn)實(shí)生活中,一個地址可能住很多 人,所以一般我們只需要根據(jù)人查到它的地址,而不太會需要從一個地址去查誰住在那里,不過,真的有這種需求的話,我們就要以用另外一種關(guān)系來實(shí)現(xiàn)了,這個 以后再講

          首先我們聲明一個人的實(shí)體類,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的實(shí)體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ù)庫里面,當(dāng)我們刪除Person的時候,也是 把這個Person對應(yīng)的Address從數(shù)據(jù)庫里面刪除,否則就需要我們?nèi)斯さ膭h除兩遍或者插入兩遍等等,optional表示這個成員是不是可選的, 我們這里是可選的,也就是說一個人可以沒有地址(比如流浪漢:)).
          然后我們發(fā)現(xiàn),在Address里面只有一些簡單的EntityBean的注釋,并沒有表示關(guān)系的注釋,這是因?yàn)楸纠且粚σ粏蜗虻膶?shí)現(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的列,也就是默認(rèn)列名是:address_id.

          如果是主鍵相關(guān)聯(lián)的話,那么可以運(yù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 "";
          }


          好了,一對一單向差不多就這么些吧,明天再看一下一對一雙向的情況.

          posted on 2008-09-03 11:51 gdufo 閱讀(350) 評論(0)  編輯  收藏 所屬分類: Hibernate

          導(dǎo)航

          統(tǒng)計

          常用鏈接

          留言簿(6)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          收藏夾

          Hibernate

          友情鏈接

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 禹州市| 即墨市| 兴业县| 乐平市| 新绛县| 林芝县| 礼泉县| 鄂尔多斯市| 丹凤县| 蓬溪县| 金湖县| 湘潭市| 汶上县| 子长县| 宣威市| 普定县| 班戈县| 龙陵县| 太保市| 绥阳县| 郴州市| 嫩江县| 房产| 师宗县| 崇左市| 牡丹江市| 连南| 临高县| 灵石县| 龙里县| 蕉岭县| 玛纳斯县| 监利县| 湾仔区| 西畴县| 清徐县| 明水县| 莆田市| 邵阳县| 项城市| 来宾市|