使用Hibernate TABLE_PER_CLASS继承时,外键会附加随机数

时间:2011-07-13 18:23:16

标签: hibernate foreign-keys hbm2ddl

我目前在我的域名模型中有以下内容:

@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@Entity
abstract class Item {
    @ManyToOne
    @ForeignKey(name="FK_ITEM_ORG")
    @JoinColumn(name="ORG_ID")
    private Organization org
}

@Table(name = "ItemA")
public class ItemA extends Item {}

@Table(name = "ItemB")
public class ItemA extends Item {}

Hibernate的HBM2DDL为此映射创建了2个表:ItemAItemB。两者都有ORG_ID列和Organization表的外键。但是,每个外键都附加一个随机数(即FK_ITEM_ORG98343)。如何指定每个表使用的外键?例如,我想要FK_ITEMA_ORGFK_ITEMB_ORG

更新

另请参阅后续问题:Is the ForeignKey annotation only used by HBM2DDL to generate the schema?

1 个答案:

答案 0 :(得分:2)

不幸的是,您必须从主类中的字段中删除注释,并将其移动到每个子项中的方法并提供FK名称。

类似的东西:

@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@Entity
public abstract class Item {
    private Organization  org;
    public Organization getOrg() {
        return org;
    }

}

@Entity
@Table(name = "ItemA")
public class ItemA extends Item {
    @ManyToOne
    @JoinColumn(name="ORG_ID")
    @ForeignKey(name="FK_ITEM_ORG_1")
    public Organization getOrg(){
        return super.getOrg();
    }
}


public class ItemB extends Item{
    @ManyToOne
    @JoinColumn(name="ORG_ID")
    @ForeignKey(name="FK_ITEM_ORG_2")
    public Organization getOrg(){
        return super.getOrg();
    }
}