1
我有兩個@Entity
一個和乙,和我有一個@Embeddadble
Address
如下:如何忽略@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
列,但我還是要嵌入Address
在B因爲它在我的模型中有意義。上述映射不適用於B,因爲由Hibernate生成的SQL始終引用line2列。
如果我在Address.line2
把@Transient
,它適用於@Entity
乙,但現在@Entity
一個失去查詢,插入和更新2號線的能力。
你認爲有可能有選擇地忽略@Embeddable
的某些屬性嗎?
有趣。並使對象模型看起來更加正確。 – Gustavo
但它不能有MappedSuperclass和Embeddable。非此即彼。此外,JPA規範沒有定義對繼承的Embeddable對象的支持(即鑑別器等,這將需要識別它們的類型) –
@NeilStockton我完全按照Naros的說法,除了「MappedSuperclass」之外。它適用於「B」實體。還沒有測試過A。 – Gustavo