2012-12-14 81 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

謝謝。奇蹟般有效。 –