@Enumerated(EnumType.STRING)因@AttributeOverride而丢失

时间:2019-05-17 14:23:39

标签: hibernate jpa jpa-2.0 embeddable

我需要在@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;
}

1 个答案:

答案 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);
    }
}