第一种方案:一个子类对应一张表
该方案是使继承体系中每一个子类都对应数据库中的一张表。
示意图如下:

每一个子类对应的数据库表都包含了父类的信息,并且包含了自己独有的属性。每个子类对应一张表,而且这个表的信息是完备的,即包含了所有从父类继承下来的属性映射的字段。这种策略是使用标签来定义子类的。
配置People.hbm.xml文件:(注意:这里都写在了一个People的配置文件里,也可以分别写在三个配置文件,即Student.hbm.xml、Teacher.hbm.xml里,以下同样可以)
- <?xml version="1.0" encoding="utf-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping>
- <class name="com.suxiaolei.hibernate.pojos.People" abstract="true">
- <id name="id" type="string">
- <column name="ID"></column>
- <generator class="uuid"></generator>
- </id>
- <property name="name" column="NAME" type="string"></property>
- <property name="sex" column="SEX" type="string"></property>
- <property name="age" column="AGE" type="string"></property>
- <property name="birthday" column="BIRTHDAY" type="timestamp"></property>
- <!--
- <union-subclass name="com.suxiaolei.hibernate.pojos.Student" table="STUDENT">
- <property name="cardId" column="CARDID" type="string"></property>
- </union-subclass>
- <union-subclass name="com.suxiaolei.hibernate.pojos.Teacher" table="TEACHER">
- <property name="salary" column="SALARY" type="integer"></property>
- </union-subclass>
- -->
- </class>
- <!-- 可单独写在Student.hbm.xml里 -->
- <union-subclass name="com.suxiaolei.hibernate.pojos.Student"
- table="STUDENT" extends="com.suxiaolei.hibernate.pojos.People">
- <property name="cardId" column="CARDID" type="string"></property>
- </union-subclass>
- <!-- 可单独写在Teacher.hbm.xml里 -->
- <union-subclass name="com.suxiaolei.hibernate.pojos.Teacher"
- table="TEACHER" extends="com.suxiaolei.hibernate.pojos.People">
- <property name="salary" column="SALARY" type="integer"></property>
- </union-subclass>
- </hibernate-mapping>
