我对使用Java和Hibernate的对象关系映射有一个奇怪的难题。不寻常的Hibernate继承映射
我们有一个现有的模式,它看起来是这样的:
create table foo (id int8, /* ... */ primary key (id));
create table bar (id int8, foo int8, /* ... */ primary key (id));
alter table bar add constraint fk_foobar foreign key (foo) references foo;
通常情况下,你会使用ManyToOne
关系映射此。
class Foo { /* ... */ }
class Bar { private Foo foo; /* ... */ }
但在我的球队里的朋友想这映射到继承关系:
class Foo { /* ... */ }
class Bar extends Foo { /* ... */ }
有没有办法使用Hibernate拉这一关?
编辑:重要的一点是,该表bar
有一个外键列foo
,这是从bar
的标识列不同。
我使用NHibernate的(休眠的端口)与C#,答案是肯定的。在NHibernate文档中有一些例子,我确定Hibernate – David 2010-02-04 16:25:38
也存在相同的问题吗?请提供反馈。 – Bozho 2010-02-11 12:38:15
我很确定答案是否定的。我敢打赌,Hibernate是硬连线的,所以如果你有继承关系,派生类表中的主键必须是基类表的外键。 – leedm777 2010-03-08 06:20:41