hibernate中联合子类要注意的问题

hibernate对于子类的映射有3种方式,这里描述的是每个具体子类一张表的方式。

这种策略是使用union-subclass标签来定义子类的。每个子类对应一张表,而且这个表的信息是完备的,即包含了所有从父类继承下来的属性映射的字段(这就是它跟joined-subclass的不同之处,joined-subclass定义的子类的表,只包含子类特有属性映射的字段)。实现这种策略的时候,有如下步骤:

1、父类用普通标签定义即可
2、子类用标签定义,在定义union-subclass的时候,需要注意如下几点:
a)Union-subclass标签不再需要包含key标签(与joined-subclass不同);
b)Union-subclass标签,既可以被class标签所包含(这种包含关系正是表明了类之间的继承关系),也可以与class标签平行。当Union-subclass标签的定义与class标签平行的时候,需要在Union-subclass标签中,添加extends属性,里面的值是父类的全路径名称;
c)子类的其它属性,像普通类一样,定义在Union-subclass标签的内部。这个时候,虽然在union-subclass里面定义的只有子类的属性,但是因为它继承了父类,所以,不需要定义其它的属性,在映射到数据库表的时候,依然包含了父类的所有属性的映射字段。

union-subclass problem:

maybe we want to use union-subclass to represent a subclass, but if you want
to use 'native' generator, it will throw exception:
MappingException: Cannot use identity column key generation with <union-subclass>
The reason is Using identity generator, you cannot share the same id seed between
different tables. And the id has to be unique whithin the hierarchy.
'Sequence' generator will be ok, but not work on MySQL.
NOTICE: 'hilo' will work, but HibernateTemplate.merge() will delay one command, so
after merge and immediately query data will not get right result.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值