我对 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 {
}
因此,如果我保留一个新的子类,我希望类型为“1”,但我得到的是“0”。当我从 BasePK 类中删除类型并直接添加到我的基类中时,它会起作用。但类型应该是 key 的一部分。
如有任何帮助,我们将不胜感激。
最佳答案
我做了一些改变,
我跳过了额外的可嵌入类,因为它们是相同的。
我必须在注释和子类的构造函数中设置类型值,否则 hibernate session 无法处理具有相同值的不同类(得到 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;
}
}
关于java - Hibernate 3.3 继承@IdClass 中的@DiscriminatorColumn,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13882242/