2012-09-26 52 views
2

動機@Entity和@Embeddable之間Inhertitance

我的應用程序發出請求基於表foo的內容,一些網絡服務。我想記錄這些請求(在數據庫中)以及foo的行的值,這是用來做它們的。儘管如此,由於表foo中的數據可能隨時間而改變,我需要記錄行的確切內容(而不是其id)。我使用Hibernate,所以我想,我可以將foo的行嵌入到表示請求的實體中。有乾淨的類層次結構,我想有這樣的事情:

class Foo 
{ 
    /* 
    * Here goes some properties of Foo, which specify columns names etc. 
    */ 
} 

@Embeddable 
class EmbeddableFoo extends Foo 
{ 
    //EmbeddableFoo should have the same properties as Foo 
} 

@Entity 
class EntityFoo extends Foo 
{ 
    @Id 
    Long getId() { //EntityFoo has some id } 
    //EntityFoo should have the same properties as Foo, except that 
    //it has id 
} 

問題

這是行不通的,因爲EmbeddableFoo看到富性能。我可以添加@Inheritance註釋過,使EntityFoo * *看到性能美孚的,但它不會解決我的問題EmbeddableFoo

問題

是有一種簡單的方法(即不需要編寫大量的冗餘代碼)可以從同一個類繼承嗎?

回答

2

你檢查了註釋@MappedSuperclass?這可能是你正在尋找的東西。

http://docs.oracle.com/javaee/5/api/javax/persistence/MappedSuperclass.html

指定一個類,其映射信息被施加到 實體從其繼承。映射的超類沒有單獨的爲其定義的 表。

與MappedSuperclass註解指定一個類可以以同樣的方式被映射 作爲一個實體,只是因爲沒有表存在的映射超 本身的映射只 適用於它的子類。當應用於子類時,繼承映射將在子類表的上下文中應用 。通過使用AttributeOverride和AssociationOverride註釋或相應的XML元素,可以在此類子類中覆蓋映射信息 。

相關問題