Hibernate4.3 继承映射

一、单表继承映射

  父子类合成一张表

  

An_id

An_name

gender

Weight

Height

type

1

dog

1

300

D

2

cat

1

100

C

  在Animal.hbm.xml配置文件中:

  

1 <!-- 鉴别器,在单表中加入一列来区分子类的 -->
2 <discriminator column="type" type="string"></discriminator>
3 <subclass name="Dog" discriminator-value="d">
4 <!-- 子类中的属性映射 -->
5  <property name="weight"></property>
6  </subclass>
7  <subclass name="Cat" discriminator-value="c">
8  <property name="height"></property>
9  </subclass>

二、父子类继承映射

  父类产生父类表,子类产生子类表

 1 <!-- 
 2             子类映射配置
 3             name:子类的名字
 4             table:子类映射表名
 5          -->
 6         <joined-subclass name="Pig" table="t_pig">
 7             <!-- 
 8                 key:字表的主键设置
 9                 column:主键名称
10              -->
11             <key column="pid"></key>
12             <!-- 子类的属性映射 -->
13             <property name="weight"></property>
14         </joined-subclass>
15         <joined-subclass name="Bird" table="t_bird">
16             <!-- 
17                 key:字表的主键设置
18                 column:主键名称
19              -->
20             <key column="bid"></key>
21             <!-- 子类的属性映射 -->
22             <property name="height"></property>
23         </joined-subclass>

父子表的映射因为生成的表多张,查询的时候我们需要多表连接查询,所以效率没有单表继承映射高

三、子表继承映射

  

 1 <!-- 字表映射,需要把父类的映射设置成抽象的(不会产生父表)
 2         abstract="true"
 3      -->
 4     <class name="Animal" table="t_animal" abstract="true">
 5         <!-- id
 6             是主键映射配置
 7          -->
 8         <id name="anId" column="an_id">
 9             <!-- 
10                 generator:主键的映射策略
11              -->
12             <generator class="uuid"></generator>
13         </id>
14         
15         <property name="anName" column="an_name"></property>
16         <property name="gender"></property>
17         
18         <!-- 子表映射 -->
19         <union-subclass name="Pig" table="t_pig">
20             <property name="weight"></property>
21         </union-subclass>
22         <union-subclass name="Bird" table="t_bird">
23             <property name="height"></property>
24         </union-subclass>
原文地址:https://www.cnblogs.com/cat-fish6/p/8678534.html