一口一口吃掉Hibernate(七)——繼承映射

來源:互聯網
上載者:User

標籤:gravity   keyword   nbsp   解釋   int   highlight   column   key   優點   

   前幾篇博文中講到了常用的幾種關聯映射。其實hibernate中還有一種“省勁兒”的映射,那就是——“繼承映射”。

 

       學了這麼多的關係映射了,繼承映射,從字面上也能瞭解到猜到幾分。沒錯,繼承映射就是為繼承樹而設定的一種映射方案。

 

 

       繼承映射的實現有三種策略:

 

  • 單表繼承。每棵類繼承樹使用一個表
  • 類表繼承。每個子類產生一個表。
  • 具體表繼承。每個類產生一個表。

 

 

      我們還是用執行個體來說明一下,直接吧:

       Pig和Bird都繼承自Animal,它們都有id,name,sex,但是Pig主要看weight,而Bird主要看height,此時如果把這些資料存入到資料庫中,可以有3種方案,正好對應3種策略。

 

       ①. 只產生一個table,對應的繼承映射策略是“單表繼承”。

       ②. 每個類產生一個table,對應的繼承策略是“類表繼承”。

      ③. 產生3張表,對應的策略是“具體表繼承”。

       相信大家更願意看代碼,那就用代碼說一下吧:

 

  1. public class Animals {  
  2.       
  3.     private int id;  
  4.       
  5.     private String name;  
  6.       
  7.     private boolean sex;  
  8.   
  9.     //此處省略get,set方法  
  10. }  
  11.   
  12. public class Bird extends Animals {  
  13.   
  14.     private int height;  
  15.       
  16.     //此處省略get,set方法  
  17. }  
  18.   
  19. public class Pig extends Animals {  
  20.   
  21.     private int weight;  
  22.       
  23.     //此處省略get,set方法  
  24. }  

 

 

       第一種對應的對應檔:使用discriminator和subclass標籤

 

  1. <?xml version="1.0"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC   
  3.     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  4.     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  5. <hibernate-mapping package="com.bjpowernode.hibernate">  
  6.     <class name="Animals" table="t_animals" lazy="false">  
  7.         <id name="id">  
  8.             <generator class="native"/>  
  9.         </id>  
  10.         <discriminator column="type" type="string"></discriminator>  
  11.         <property name="name"/>  
  12.         <property name="sex"></property>  
  13.         <subclass name="Pig"  discriminator-value="P">  
  14.             <property name="weight"></property>  
  15.         </subclass>  
  16.         <subclass name="Bird" discriminator-value="B">  
  17.             <property name="height"></property>  
  18.         </subclass>  
  19.     </class>  
  20. </hibernate-mapping>  

 

 

       discriminator,英文解釋是“辨別器”。它是用於設定辨別資料的欄位,放在id下面,同時使用subclass來配置不同的類,設定discriminator-value,來辨別不同的資料。

 

      第二種對應檔:使用union-subclass標籤

 

  1. <span style="font-size:18px"><?xml version="1.0"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC   
  3.     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  4.     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  5. <hibernate-mapping package="com.bjpowernode.hibernate">  
  6.     <class name="Animals" table="t_animals" abstract="true">  
  7.         <id name="id">  
  8.             <!-- 不能使用native,因為自增會出現兩個表一樣的id -->  
  9.             <generator class="assigned"/>  
  10.         </id>  
  11.         <property name="name"/>  
  12.         <property name="sex"/>  
  13.         <union-subclass name="Pig" table="t_pig">  
  14.             <property name="weight" />  
  15.         </union-subclass>  
  16.         <union-subclass name="Bird" table="t_bird">  
  17.             <property name="height" />  
  18.         </union-subclass>  
  19.     </class>  
  20. </hibernate-mapping></span>  

 

      第三種對應檔:使用joined-subclass標籤

 

  1. <?xml version="1.0"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC   
  3.     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  4.     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  5. <hibernate-mapping package="com.bjpowernode.hibernate">  
  6.     <class name="Animals" table="t_animals" lazy="false">  
  7.         <id name="id">  
  8.             <generator class="native"/>  
  9.         </id>  
  10.         <property name="name"/>  
  11.         <property name="sex"/>  
  12.         <joined-subclass name="Pig" table="t_pig">  
  13.             <key column="pid" />  
  14.             <property name="weight" />  
  15.         </joined-subclass>  
  16.         <joined-subclass name="Bird" table="t_bird">  
  17.             <key column="bid" />  
  18.             <property name="height" />  
  19.         </joined-subclass>  
  20.     </class>  
  21. </hibernate-mapping>  

 

 

       以上三種策略,各有優缺點:

 

    • 單表繼承。查詢速度最快,效率高,但是存在冗餘欄位。
    • 類表繼承。層次清楚,無冗餘;但是如果使用自增主鍵方式會產生重複主鍵。需要使用手動分配主鍵。
    • 具體表繼承。層次最明了,這是優點,也是缺點,而且資料操作效率不高。

一口一口吃掉Hibernate(七)——繼承映射

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.