2012-12-14 83 views
2

我有id类中的鉴别器列的继承问题。该表将被创建成功,但每个条目在descriminator列中都会获得“0”值。Hibernate 3.3继承@IdClass中的@DiscriminatorColumn

这里是我的基类:

@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类中删除类型并直接添加到我的基类中时起作用。但类型应该是关键的一部分。

任何帮助将不胜感激。

回答

1

我做了一些更改,

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

我必须在注释和子类的构造函数中设置类型值,否则hibernate会话将无法处理具有相同值的不同类(得到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; 
    } 
} 
+1

谢谢。奇迹般有效。 –