2014-02-18 109 views
0

如何在多繼承實體組合鍵中使用IdClass?JPA繼承IdClass

@IdClass(IdClassEntity1.class) 
    class Entity1 { 
    private long e1pk1; 
    private long e1pk2; 
    } 
    @IdClass(IdClassEntity2.class) 
    class Entity2 extends Entity1 { 
    private long e2pk1; 
    private long e2pk2; 
    } 
    class Entity3 extends Entity2 { 
    private long e3pk1; 
    private long e3pk2; 
    } 

應該是什麼IdClassEntity2:

class IdClassEntity2 { 
     private long e1pk1; 
     private long e1pk2; 
     private long e2pk1; 
     private long e2pk2; 
} 

class IdClassEntity2 { 
     private IdClassEntity1 idClassEntity1; 
     private long e2pk1; 
     private long e2pk2; 
} 
+0

他們是默認映射,只需添加MappedSuperClass或@Entity,不需要重新定義在子類中。 – Koitoer

+0

問題更新 – hiddenuser

回答

1

您可以使用@MappedSuperClass@Entity申報@IdClass。 PK通過繼承傳播。

例如,使用@MappedSuperClass,你可以做到以下幾點:

@MappedSuperClass 
@IdClass(IdClassEntity1.class) 
public class Entity1 { 
    @Id private long e1pk;  
    @Id private long e1pk; 
    ... 

@Entity 
public class Entity2 extends Entity1 { 
    ... 

使用@Entity,遵循相同的模式

+0

因此,SubClass Hierarchy不能定義他們自己的Pk與超級PK意味着:超級(PK1,PK2),子類1(PK3)和子類2(PK4) – hiddenuser

+2

@隱藏用戶:不,他們不能定義他們的PK。層次結構中的所有實體類都與'@ MappedSuperclass'實體共享相同的組合鍵,以便您可以在其上調用'em.find()'。從數據庫角度來看,層次結構中的所有實體實例都存儲在同一個表中,並且每個實體類型都由鑑別器識別。考慮到你不能實例化和查詢'Entity1',因爲它不是持久的。 – wypieprz