瘋狂

          STANDING ON THE SHOULDERS OF GIANTS
          posts - 481, comments - 486, trackbacks - 0, articles - 1
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          hibernate annoation (七 繼承映射)

          Posted on 2009-11-02 14:54 瘋狂 閱讀(2163) 評(píng)論(0)  編輯  收藏 所屬分類: hibernate

          Table per Class Strategy: the <union-class> element in Hibernate
          Single Table per Class Hierarchy Strategy: the <subclass> element in Hibernate
          Joined Subclass Strategy: the <joined-subclass> element in Hibernate
          ejb支持三種映射關(guān)系
          1,每個(gè)類一張表 (hibertnate里對(duì)應(yīng)<union-class>)
          2,每個(gè)類層次一張表 (在 hibernate里對(duì)應(yīng)<subclass>)
          3,連接的子類(對(duì)應(yīng)join-subclass)

             目前不支持在接口上進(jìn)行注解
          (1)每個(gè)類一張表:
          在父類class-level上設(shè)置:@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
          例如:

          Java代碼 復(fù)制代碼
          1. class A代碼:   
          2. @Entity  
          3. @Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)   
          4. public class A {   
          5.   
          6.  private int id;   
          7.  private String aname;   
          8.  @Id  
          9.  @GeneratedValue(strategy=GenerationType.IDENTITY)   
          10.  public int getId() {   
          11.   return id;   
          12.  }   
          13.  public void setId(int id) {   
          14.   this.id = id;   
          15.  }   
          16.  public String getAname() {   
          17.   return aname;   
          18.  }   
          19.  public void setAname(String aname) {   
          20.   this.aname = aname;   
          21.  }   
          22.     
          23. }   
          24.   
          25. class B extends A代碼:   
          26. @Entity  
          27. public class B extends A{   
          28.   
          29.  private String bname;   
          30.   
          31.  public String getBname() {   
          32.   return bname;   
          33.  }   
          34.   
          35.  public void setBname(String bname) {   
          36.   this.bname = bname;   
          37.  }   
          38.     
          39. }   
          40. class C extends A代碼:   
          41. @Entity  
          42. public class C extends A{   
          43.   
          44.  private String cname;   
          45.   
          46.   
          47.  public String getCname() {   
          48.   return cname;   
          49.  }   
          50.   
          51.  public void setCname(String cname) {   
          52.   this.cname = cname;   
          53.  }   
          54.   
          55.     
          56. }  

           

          最終生成sql語句:

          Java代碼 復(fù)制代碼
          1. create table A (id integer not null auto_increment, aname varchar(255), primary key (id))   
          2. create table B (id integer not null, aname varchar(255), bname varchar(255), primary key (id))   
          3. create table C (id integer not null, aname varchar(255), cname varchar(255), primary key (id))  

           

          B 和 C 都繼承了A但是沒有關(guān)聯(lián)

          (2)每個(gè)類層次一張表:也就是所有繼承的類和父類共享一張表 通過一個(gè)辨別符號(hào)進(jìn)行區(qū)分
          這需要在父類上使用:@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
          這樣的話在表里面會(huì)多出一個(gè)字段:DTYPE(默認(rèn) 默認(rèn)值為entry.class)
          可以通過在父類class-level上設(shè)置
          @DiscriminatorColumn(name="mytype",discriminatorType=DiscriminatorType.STRING)
          在之類上使用
          例如:
          @Entity
          @DiscriminatorValue(value="ctype")
          插入數(shù)據(jù)時(shí)候?qū)?huì)有下列語句產(chǎn)生:Hibernate: insert into A (aname, cname, mytype) values (?, ?, 'ctype');

          (3)每個(gè)字類一張表:也就是字類的關(guān)聯(lián)到父類的主鍵
           通過在父類上使用:@Inheritance(strategy=InheritanceType.JOINED)
           產(chǎn)生語句:默認(rèn)id關(guān)聯(lián)

          Java代碼 復(fù)制代碼
          1. create table A (id integer not null auto_increment, aname varchar(255), primary key (id))   
          2. create table B (bname varchar(255), id integer not null, primary key (id))   
          3. create table C (cname varchar(255), id integer not null, primary key (id))   
          4. alter table B add index FK42FCA55807 (id), add constraint FK42FCA55807 foreign key (id) references A (id)   
          5. alter table C add index FK43FCA55807 (id), add constraint FK43FCA55807 foreign key (id) references A (id)  

           
          也可以指定關(guān)聯(lián) 例如:在B的class-level上使用@PrimaryKeyJoinColumn(name="bid")
          生成sql語句:

          Java代碼 復(fù)制代碼
          1. create table B (bname varchar(255), bid integer not null, primary key (bid))   
          2. alter table B add index FK42FCA6C7E9 (bid), add constraint FK42FCA6C7E9 foreign key (bid) references A (id)  

           
          但是我們不能關(guān)聯(lián)到A的非主鍵字段例如:
          在B上使用
          @PrimaryKeyJoinColumn(name="bid",referencedColumnName="aname")則會(huì)報(bào)錯(cuò):SecondaryTable JoinColumn cannot reference a non primary key
            當(dāng)然也可以給之類關(guān)聯(lián)設(shè)置不同的類型例如:@PrimaryKeyJoinColumn(name="bid",columnDefinition="carchar(20)")但是不能設(shè)置不能轉(zhuǎn)換的類型例如:
          @PrimaryKeyJoinColumn(name="bid",columnDefinition="blob")則會(huì)建立不了關(guān)聯(lián)

          (4)從實(shí)體繼承 但是父類不持久化:使用@MappedSuperclass
          sql語句:

          Java代碼 復(fù)制代碼
          1. create table B (id integer not null auto_increment, aname varchar(255), bname varchar(255), primary key (id))   
          2. create table C (id integer not null auto_increment, aname varchar(255), cname varchar(255), primary key (id))  

           
           當(dāng)然可以使用@AttributeOverride或者@AssociationOverride進(jìn)行覆蓋

          主站蜘蛛池模板: 垣曲县| 错那县| 彝良县| 教育| 溧阳市| 石河子市| 禄丰县| 鹿邑县| 庆阳市| 乌拉特中旗| 梁河县| 朝阳市| 工布江达县| 崇明县| 延吉市| 敖汉旗| 静乐县| 刚察县| 金沙县| 西林县| 德钦县| 武平县| 阿勒泰市| 萨迦县| 龙泉市| 聂荣县| 铁岭市| 盐津县| 蚌埠市| 衡阳市| 临西县| 洪泽县| 龙川县| 台北市| 高碑店市| 焉耆| 通化县| 福鼎市| 泉州市| 五河县| 双辽市|