2

我在 id 类中的鉴别器列存在继承问题。该表将成功创建,但每个条目在描述符列中获得“0”值。

这是我的基类:

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.INTEGER)
@IdClass(BasePK.class)
@SuppressWarnings("serial")
public abstract class Base implements Serializable {

@Id
protected Test test;

@Id
protected Test2 test2;

@Id
private int type;

....
}

这是我的基础 pk 课程:

@Embeddable
public static class BasePK implements Serializable {

@ManyToOne
protected Test test;

@ManyToOne
protected Test2 test2;

@Column(nullable = false)
protected int type;

...
}

我有几个这样的子类:

@Entity
@DiscriminatorValue("1")
@SuppressWarnings("serial")
public class Child extends Base {

}

因此,如果我坚持一个新的 Child 类,我希望将“1”作为类型,但我得到“0”。当我从 BasePK 类中删除类型并直接添加到我的 Base 类中时,它可以工作。但是类型应该是键的一部分。

任何帮助将不胜感激。

4

1 回答 1

1

我做了一些改变,

我跳过了额外的可嵌入类,因为它们是相同的。

我必须在注释和子类的构造函数中设置类型值,否则休眠会话无法处理具有相同值的不同类(得到 NotUniqueObjectException)。

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.INTEGER)
@IdClass(Base.class)
public abstract class Base implements Serializable {
    @Id @ManyToOne protected Test test;
    @Id @ManyToOne protected Test2 test2;
    @Id private int type;
}

@Entity
@DiscriminatorValue("1")
public class Child1 extends Base {
    public Child1(){
        type=1;
    }
}

@Entity
@DiscriminatorValue("2")
public class Child2 extends Base {
    public Child2(){
        type=2;
    }
}
于 2012-12-14T17:24:02.887 回答