关于Hibernate的hilo主键生成器(另附继承映射)

-------------------- 题记

因为项目需要使用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>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值