我需要在@AttributeOverride
对象上使用@Embeddable
,以防止相同复杂类型的两个属性的列名冲突。
但是,这样做时,我也会丢失/覆盖@Enumerated
networkId
对象的@Embedded
属性的ExternalLink
注释。
最后,表的列link_network
的类型为integer
,而不是varyingchar(255)
。
如何配置@AttributeOverride
以保持@Enumerated
的配置?
这是我的模式:
@Embeddable
public class Address {
@Embedded
@AttributeOverrides({
@AttributeOverride(name="network", column=@Column(name="link_network")),
@AttributeOverride(name="networkAddress", column=@Column(name="link_network_address")),
@AttributeOverride(name="element", column=@Column(name="link_element"))
})
private ExternalLink link;
@Embedded
@AttributeOverrides({
@AttributeOverride(name="network", column=@Column(name="before_network")),
@AttributeOverride(name="networkAddress", column=@Column(name="before_network_address")),
@AttributeOverride(name="element", column=@Column(name="before_element"))
})
private ExternalLink before;
}
@Embeddable
public class ExternalLink {
@Enumerated(EnumType.STRING)
private NetworkId network;
private String networkAddress;
private String element;
}
答案 0 :(得分:1)
我唯一知道的方法是转换器,它将NetworkId
映射到String
(varchar)
@Converter(autoApply = true)
public class NetworkIdConverter implements AttributeConverter<NetworkId , String> {
@Override
public String convertToDatabaseColumn(NetworkId attribute) {
if (attribute == null) {
return null;
}
return attribute.name();
}
@Override
public NetworkId convertToEntityAttribute(String dbData) {
if (dbData == null) {
return null;
}
return NetworkId.valueOf(dbData);
}
}