hibernate中奇怪的遗留继承

时间:2012-01-26 11:12:05

标签: hibernate inheritance

我在sql中有这样的表:

id = 1, type = 1, description = "something"
id = 2, type = 1, description = "something else"
id = 1, type = 2, description = "another class"
id = 2, type = 2, description = "something"

我想创建两个继承自同一个类的类。并且鉴别器应该是类型。问题是可以为每个子类型重复id。

当我将@ManyToOne与其他类关联时,Hibernate不使用type,只有id。

1 个答案:

答案 0 :(得分:0)

class Base
{
    // map these two as compositeId
    protected int id;
    protected int type;
}

class A extends Base
{
    public A()
    {
        type = 1;
    }
}

@JoinColumns(... a_id, a_type ...)
property A referencedA;

class Base
{
    // map as id
    protected int id;

    // map to type
    protected int type;
}

@where("type=1")
class A extends Base
{
    public A()
    {
        type = 1;
    }
}

@Many-to-any(... idcolumn: "a_id", typecolumn: "a_type" ...)
public A referencedA;