-------------------- 题记
因为项目需要使用hibernate继承映射,于是测试了hibernate的三种继承映射:subclass、join和union。在union的测试中,基类的主键生成器不能使用identity,而我们使用的数据库DB2又没有sequence,所以必须使用hilo(high-low)主键生成器。
所谓的hilo就是使用一个高/低位算法来高效的生成long、short或int类型的标识符。给定一个表和字段(默认分别是hibernate_unique_key和next_hi)作为高地位的来源。高/低位算法生成的标识属性值只在特定的数据库中是唯一的。
注:默认的表hibernate_unique_key需要自己建立,而且必须拥有默认的字段next_hi,当然也可以使用自己喜欢的表名和喜欢的字段,但必须在hbm配置文件中添加param描述(百度hibernate的hilo)。另外,因为此表在数据库中是唯一的,所以标识属性值在特定的数据库中是唯一的。
-------------------- 代码示例(只展示hbm文件和数据库表格)
数据库表格:
-- =========== subclass ============
-- drop table t_test_machine_subclass;
create table t_test_machine_subclass(
pk_mac_id int generated always as identity (start with 1, increment by 1) primary key not null,
f_mac_name varchar(50),
f_cpt_mouse varchar(50),
f_tv_bigScreen varchar(50),
f_type varchar(50)
);
-- =========== join ============
-- drop table t_test_machine_join;
create table t_test_machine_join(
pk_mac_id int generated always as identity (start with 1, increment by 1) primary key not null,
f_mac_name varchar(50)
);
-- drop table t_test_computer_join;
create table t_test_computer_join(
pk_fk_cpt_id int primary key not null,
f_cpt_mouse varchar(50)
);
-- drop table t_test_television_join;
create table t_test_television_join(
pk_fk_tv_id int primary key not null,
f_tv_bigScreen varchar(50)
);
-- =========== union ============
-- drop table hibernate_unique_key;
create table hibernate_unique_key(
next_hi int
);
insert into hibernate_unique_key values (0);
-- drop table t_test_machine_union;
create table t_test_machine_union(
pk_mac_id int primary key not null,
f_mac_name varchar(50)
);
-- drop table t_test_computer_union;
create table t_test_computer_union(
pk_mac_id int primary key not null,
f_mac_name varchar(50),
f_cpt_mouse varchar(50)
);
-- drop table t_test_television_union;
create table t_test_television_union(
pk_mac_id int primary key not null,
f_mac_name varchar(50),
f_tv_bigScreen varchar(50)
);
hbm文件:
<?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 package="com.llw.model.extend">
<!-- subclass -->
<class name="Machine" table="t_test_machine_subclass" discriminator-value="普通机器" >
<id name="machineId" type="int" column="pk_mac_id" >
<generator class="native" />
</id>
<discriminator type="string" column="f_type" />
<property name="machineName" type="string" column="f_mac_name" />
<subclass name="Computer" discriminator-value="电脑" >
<property name="mouse" type="string" column="f_cpt_mouse" />
</subclass>
<subclass name="Television" discriminator-value="电视" >
<property name="bigScreen" type="string" column="f_tv_bigScreen" />
</subclass>
</class>
<!-- join -->
<class name="Machine" table="t_test_machine_join" >
<id name="machineId" type="int" column="pk_mac_id" >
<generator class="native" />
</id>
<property name="machineName" type="string" column="f_mac_name" />
<joined-subclass name="Computer" table="t_test_computer_join">
<key column="pk_fk_cpt_id" />
<property name="mouse" type="string" column="f_cpt_mouse" />
</joined-subclass>
<joined-subclass name="Television" table="t_test_television_join">
<key column="pk_fk_tv_id" />
<property name="bigScreen" type="string" column="f_tv_bigScreen" />
</joined-subclass>
</class>
<!-- union -->
<class name="Machine" table="t_test_machine_union" >
<id name="machineId" type="int" column="pk_mac_id" >
<generator class="hilo" />
</id>
<property name="machineName" type="string" column="f_mac_name" />
<union-subclass name="Computer" table="t_test_computer_union" >
<property name="mouse" type="string" column="f_cpt_mouse" />
</union-subclass>
<union-subclass name="Television" table="t_test_television_union" >
<property name="bigScreen" type="string" column="f_tv_bigScreen" />
</union-subclass>
</class>
</hibernate-mapping>