13 hibernate 继承映射
- 2. 继承映射策略
Hibernate 支持三种基本的继承映射策略:
每棵继承树 ( 类分层结构 ) 映射成一张表
(table per class hierarchy)
每个子类映射成一张表 (table per subclass)
每个具体类映射成一张表 (table per concrete class)
在映射文件中分别使用 subclass , union-subclass
和 joined-subclass 标签来定义
www.sodi.com.cn
- 3. 继承映射实体对象
Animal( 父类 )
id: int
name:String
sex: int
Pig( 子类 ) Bird( 子类 )
weight: double height: double
www.sodi.com.cn
- 4. 第一种方式
每棵继承树 ( 类分层结构 ) 映射成一张表
关系模型 : ( 表结构 )
Animal( 表 )
id name sex weight height type
------------------------------------------------------------------------------------
1 小胖 0 100 P
2 大飞 1 2000 B
3 胖胖 1 500 P
4 花花 0 1800 B
……
www.sodi.com.cn
- 5. 映射文件实现
Animal.hbm.xml:
<class name="Animal" table="tab_animal">
<id name="id" column="id">
<generator class="assigned"/>
</id>
<!-- 定义一个鉴别器 -->
<discriminator column="type" type="string" />
<property name="name" column="name"/>
<property name="sex" column="sex" />
<!-- 定义子类,这个标签放置在 class 外面,加入 extends 属性指定
父类 -->
<subclass name="Pig" discriminator-value="P">
<property name="weight" column="weight" />
</subclass>
<!-- 定义子类,加入鉴别值 -->
<subclass name="Bird" discriminator-value="B">
<property name="height" column="height" />
</subclass>
</class>
www.sodi.com.cn
- 6. 操作演示
操作演示
www.sodi.com.cn
- 7. 第二种方式
每个子类映射成一张表
关系模型 : ( 表结构 )
Pig( 表 )
id weight
---------------------------- Animal( 表 )
1 100
3 500
id name sex
……
-----------------------------------------
1 小胖 0
2 大飞 1
Bird( 表 ) 3 胖胖 1
4 花花 0
id height ……
----------------------------
2 1000
4 2000
……
www.sodi.com.cn
- 8. 映射文件实现
Animal.hbm.xml:
<class name="Animal" table="tab_animals" >
<id name="id" column="id">
<generator class="assigned"/>
</id>
<property name="name" column="name"/>
<property name="sex" column="sex" />
</class>
<!– 连接的子类 -->
<joined-subclass name="Pig" table="tab_pig" extends="Animal">
<key column=“id”/> <!-- 这个标签指定外键字段 -->
<property name="weight" column="weight"/>
</joined-subclass>
<joined-subclass name="Bird" table="tab_bird" extends="Animal">
<key column="id"/> <!-- 这个标签指定外键字段 -->
<property name="height" column="height"/>
</joined-subclass>
www.sodi.com.cn
- 9. 操作演示
操作演示
www.sodi.com.cn
- 10. 第三种方式
每个具体类映射成一张表
关系模型 : ( 表结构 )
Pig( 表 )
id name sex weight
---------------------------------------------------------------------
1 小胖 0 100
3 胖胖 1 500
……
Bird( 表 )
id name sex height
----------------------------------------------------------------------
2 大飞 1
1000
4 花花 0
2000
……
www.sodi.com.cn
- 11. 映射文件实现
Animal.hbm.xml:
<!-- 无须指定 table 属性 -->
<class name="Animal" lazy="false" >
<id name="id" column="id">
<generator class="assigned"/>
</id>
<property name="name" column="name"/>
<property name="sex" column="sex" />
<union-subclass name="Pig" table="tab_pigs">
<property name="weight" column="weight"/>
</union-subclass>
<union-subclass name="Bird" table="tab_birds">
<property name="height" column="height"/>
</union-subclass>
</class>
www.sodi.com.cn
- 12. 操作演示
操作演示
www.sodi.com.cn
- 13. 三种方式的优缺点
每棵继承树 ( 类分层结构 ) 映射成一张表
性能最优,但会产生冗余字段,且必须相关字段设为
非空
每个子类映射成一张表
层次比较清晰,但需要连接查询,性能会受影响
每个具体类映射成一张表
表中会产生冗余字段,主键字段不能使用自增
www.sodi.com.cn