2015-12-30 25 views
1

我有兩個@Entity一個,和我有一個@EmbeddadbleAddress如下:如何忽略@Entity中@Embeddable的某些屬性?

@Embeddable 
public class Address { 
    private String line1; 
    private String line2; 
    ... 
} 

@Entity 
public class A { 
    @Id private long id; 

    @Embedded 
    @AttributeOverrides({ 
     @AttributeOverride(name="line1", [email protected](name="TABLE_A_LINE1")), 
     @AttributeOverride(name="line2", [email protected](name="TABLE_A_LINE2")) 
    }) 
    private Address address; 
} 

@Entity 
public class B { 
    @Id private long id; 

    @Embedded 
    @AttributeOverrides({ 
     @AttributeOverride(name="line1", [email protected](name="TABLE_B_LINE1")) 
     // don't want line2 in B 
    }) 
    private Address address; 
} 

@Entity表沒有TABLE_B_LINE2列,但我還是要嵌入AddressB因爲它在我的模型中有意義。上述映射不適用於B,因爲由Hibernate生成的SQL始終引用line2列。

如果我在Address.line2@Transient,它適用於@Entity,但現在@Entity一個失去查詢,插入和更新2號線的能力。

你認爲有可能有選擇地忽略@Embeddable的某些屬性嗎?

回答

0

爲什麼不改造您Address略如下:

@Embeddable 
public class ExtendedAddress extends Address { 
    private String line2; 
} 

然後在你的實體,然後在你的實體使用ExtendedAddress使用Address

@MappedSuperclass 
@Embeddable 
public class Address { 
    /* does not include address line 2 */ 
} 

如下然後擴展它A

+0

有趣。並使對象模型看起來更加正確。 – Gustavo

+0

但它不能有MappedSuperclass和Embeddable。非此即彼。此外,JPA規範沒有定義對繼承的Embeddable對象的支持(即鑑別器等,這將需要識別它們的類型) –

+0

@NeilStockton我完全按照Naros的說法,除了「MappedSuperclass」之外。它適用於「B」實體。還沒有測試過A。 – Gustavo