瘋狂

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

          hibernate annoation (七 繼承映射)

          Posted on 2009-11-02 14:54 瘋狂 閱讀(2163) 評論(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支持三種映射關系
          1,每個類一張表 (hibertnate里對應<union-class>)
          2,每個類層次一張表 (在 hibernate里對應<subclass>)
          3,連接的子類(對應join-subclass)

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

          Java代碼 復制代碼
          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代碼 復制代碼
          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但是沒有關聯

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

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

          Java代碼 復制代碼
          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)  

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

          Java代碼 復制代碼
          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)  

           
          但是我們不能關聯到A的非主鍵字段例如:
          在B上使用
          @PrimaryKeyJoinColumn(name="bid",referencedColumnName="aname")則會報錯:SecondaryTable JoinColumn cannot reference a non primary key
            當然也可以給之類關聯設置不同的類型例如:@PrimaryKeyJoinColumn(name="bid",columnDefinition="carchar(20)")但是不能設置不能轉換的類型例如:
          @PrimaryKeyJoinColumn(name="bid",columnDefinition="blob")則會建立不了關聯

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

          Java代碼 復制代碼
          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))  

           
           當然可以使用@AttributeOverride或者@AssociationOverride進行覆蓋

          主站蜘蛛池模板: 涞水县| 平昌县| 万源市| 大丰市| 安庆市| 青铜峡市| 雅安市| 屯门区| 绩溪县| 泸定县| 左权县| 荆门市| 龙陵县| 博客| 沁源县| 霍林郭勒市| 丘北县| 遂宁市| 鄄城县| 长丰县| 台东市| 乌恰县| 沙坪坝区| 水城县| 安龙县| 开原市| 三江| 揭阳市| 永康市| 石河子市| 双牌县| 辛集市| 偃师市| 台湾省| 桐城市| 开封县| 中牟县| 香格里拉县| 崇文区| 玛纳斯县| 松滋市|