本文主要是学习hibernate的继承映射。
继承是面向对象的核心思想,但是关系数据库中表与表之间是没有继承关系的,下面来学习hibernate是如何将对象领域的继承关系映射到表结构的,hibernate给我们提供了三种思路,主要是基于表结构的方案决定配置文件的写法。下面的学习围绕对象领域的People,Student,Teacher这三者的继承关系来进行。
实体类定义如下(省略了get/set,以及构造函数的定义):
public class People {
private long id;
private String name;
private byte age;
private boolean sex;
private String address;
}
public class Teacher extends People implements java.io.Serializable {
private double salary;
}
public class Student extends People implements java.io.Serializable {
private String cardid;
}
1、一个子类对应一张表,比如Student一张表,Teacher一张表,配置文件如下:
<hibernate-mapping>
<class name="com.study.hbm.People" abstract="true" catalog="hibernate">
<id name="id" type="long">
<column name="id" />
<generator class="assigned" />
</id>
<property name="name" type="string">
<column name="name" length="64" not-null="true" />
</property>
<property name="age" type="byte">
<column name="age" not-null="true" />
</property>
<property name="sex" type="boolean">
<column name="sex" not-null="true" />
</property>
<property name="address" type="string">
<column name="address" length="128" not-null="true" />
</property>
</class>
<union-subclass name="com.study.hbm.Student" table="student"
extends="com.study.hbm.People">
<property name="cardid" type="string">
<column name="cardid" length="128" not-null="true" unique="true" />
</property>
</union-subclass>
<union-subclass name="com.study.hbm.Teacher" table="teacher"
extends="com.study.hbm.People">
<property name="salary" type="double">
<column name="salary" precision="22" scale="0" not-null="true" />
</property>
</union-subclass>
</hibernate-mapping>
主要是使用了union-subclass元素,注意:若union-subclass可以放置在class标签中,这时可以省去extends属性,否则必须加上extends属性,另外People的abstract属性定义为true,表示没有表结构,若为false则表示有表结构,但是不会有数据。
2、使用一张表表示继承关系,定义一张People表,使用一个标示字段来区分是Student还是Teacher,配置文件如下:
<hibernate-mapping>
<class name="com.study.hbm.People" table="people" catalog="hibernate">
<id name="id" type="long">
<column name="id" />
<generator class="assigned" />
</id>
<discriminator column="ptype" type="string"></discriminator>
<property name="name" type="string">
<column name="name" length="64" not-null="true" />
</property>
<property name="age" type="byte">
<column name="age" not-null="true" />
</property>
<property name="sex" type="boolean">
<column name="sex" not-null="true" />
</property>
<property name="address" type="string">
<column name="address" length="128" />
</property>
<subclass name="com.study.hbm.Student" discriminator-value="student">
<property name="cardid" column="cardid" type="string"></property>
</subclass>
<subclass name="com.study.hbm.Teacher" discriminator-value="teacher">
<property name="salary" column="salary" type="java.lang.Double"></property>
</subclass>
</class>
</hibernate-mapping>
<discriminator>标签用于在表中创建一个标识列,其"column"属性指定标识列的列名,"ptype"指定了标识列的类型,注意这个ptype的值类型必须是string类型。<subclass>标签用于指 定该HBM文件代表类的子类,有多少子类就有多少个该标签,其"name"属性指定子类的名称,"discriminator-value"属性指定该子类的数据的标识列的值是什么,其"extends"属性与<union-subclass>的"extends"属性用法一致。
3、每个类一张表,即定义三张表,people,student,teacher,其中student和teacher只有id和自己的属性字段,公共的属性定义在people中,它们与父类以一对一主键关联,配置文件如下:
<hibernate-mapping>
<class name="com.study.hbm.People" table="people" catalog="hibernate">
<id name="id" type="long">
<column name="id" />
<generator class="assigned" />
</id>
<discriminator column="ptype" type="string"></discriminator>
<property name="name" type="string">
<column name="name" length="64" not-null="true" />
</property>
<property name="age" type="byte">
<column name="age" not-null="true" />
</property>
<property name="sex" type="boolean">
<column name="sex" not-null="true" />
</property>
<property name="address" type="string">
<column name="address" length="128" />
</property>
<joined-subclass name="com.study.hbm.Student" table="student">
<key column="id"></key>
<property name="cardid" column="cardid" type="string"></property>
</joined-subclass>
<joined-subclass name="com.study.hbm.Teacher" table="teacher">
<key column="id"></key>
<property name="salary" column="salary" type="java.lang.Double"></property>
</joined-subclass>
</class>
</hibernate-mapping>
<joined-subclass>标签需要包含一个key标签,这个标签指定了子类和父类之间是通过哪个字段来关联的。
总结:
1)第一种方式,每个子类一个表,OID定义在People中,这样就需要子类的id主键不能重复(数据量大的话这种方式可以考虑,主键由程序生成,保证不重复)
2)第二种方式,一个表,查询效率高,有无用字段,但是整体性能高(推荐)
3)第三种方式,每个类一个表,Teacher和Student的Id是外键关联People表的主键Id,结构清晰,但是查询需要进行表关联,效率没有第二种方式高,