zoukankan      html  css  js  c++  java
  • 一口一口吃掉Hibernate(七)——继承映射

       前几篇博文中讲到了常用的几种关联映射。其实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>  

           以上三种策略,各有优缺点:

      • 单表继承。查询速度最快,效率高,但是存在冗余字段。
      • 类表继承。层次清楚,无冗余;但是如果使用自增主键方式会产生重复主键。需要使用手动分配主键。
      • 具体表继承。层次最明了,这是优点,也是缺点,而且数据操作效率不高。
  • 相关阅读:
    项目1:即时标记
    每天一个小程序—0000题(python图像处理)
    每天一个小程序—0004题(统计单词出现次数)
    每天一个小程序—第0001题(uuid模块)
    python实现八皇后问题
    2018/01/05——时间会改变一切!
    HDU 6249 Alice’s Stamps(dp)
    51nod 1422 沙拉酱前缀
    【转】DSP动态内存分配函数的使用
    优秀人的博客
  • 原文地址:https://www.cnblogs.com/KingIceMou/p/7100990.html
Copyright © 2011-2022 走看看