我有两个@Entity
A 和 B ,我有一个@Embeddadble
Address
,如下所示:
@Embeddable
public class Address {
private String line1;
private String line2;
...
}
@Entity
public class A {
@Id private long id;
@Embedded
@AttributeOverrides({
@AttributeOverride(name="line1", column=@Column(name="TABLE_A_LINE1")),
@AttributeOverride(name="line2", column=@Column(name="TABLE_A_LINE2"))
})
private Address address;
}
@Entity
public class B {
@Id private long id;
@Embedded
@AttributeOverrides({
@AttributeOverride(name="line1", column=@Column(name="TABLE_B_LINE1"))
// don't want line2 in B
})
private Address address;
}
@Entity
B 的表格没有TABLE_B_LINE2
列,但我仍想在{strong> B 中嵌入Address
,因为它在我的模型中有意义。上面的映射不适用于 B ,因为Hibernate生成的SQL总是引用line2列。
如果我将@Transient
放入Address.line2
,则适用于@Entity
B ,但现在@Entity
A 失去查询,插入和更新第2行的能力。
您是否认为有可能有选择地忽略@Embeddable
的某些属性?
答案 0 :(得分:0)
为什么不按照以下方式轻微改造Address
:
@MappedSuperclass
@Embeddable
public class Address {
/* does not include address line 2 */
}
然后按如下方式扩展:
@Embeddable
public class ExtendedAddress extends Address {
private String line2;
}
然后在您的实体 B 中使用Address
,然后在您的实体 A 中使用ExtendedAddress
。