0

我有想要覆盖实体子类中的 AttributeConverter 的问题,但子类中定义的转换器没有被调用。根据 AttributeConverter 文档,这应该是这样,但它对我不起作用。我究竟做错了什么?

@Entity
@org.hibernate.annotations.DynamicUpdate(value = true)
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "DISCRIMINATOR", discriminatorType = DiscriminatorType.STRING)
@DiscriminatorValue("ORDER")
public class Order implements Serializable
{
...
    @Column(name = "PRODUCT_SERIALIZED", updatable = false)
    @Convert(converter = ProductConverter.class)
    protected Product product;
...
}
@Entity
@DiscriminatorValue("CUSTOMER_ORDER")
@Convert(attributeName = "product", converter = CustomerProductConverter.class)
public class CustomerOrder extends Order
{
...
4

1 回答 1

0

似乎@Convert 不适合覆盖超类字段的现有转换器。我以不同的方式解决了它。我通过 CDI 将一个转换服务注入到超类的 AttributeConverter 中,然后我可以专门化它。

@Converter
public class ProductConverter implements AttributeConverter<Product, String>
{
    ProductConverterService converterBean = null;

    @Override
    public String convertToDatabaseColumn(Product attribute)
    {
        return getConverterService().convertToDatabaseColumn(attribute);
    }

    @Override
    public Product convertToEntityAttribute(String dbData)
    {
        return getConverterService().convertToEntityAttribute(dbData);
    }

    public ProductConverterService getConverterService()
    {
        if (converterBean == null)
        {
            //since ProductConverter is obiously not managed via CDI
            converterBean = CDI.current().select(ProductConverterService.class).get();
        }
        return converterBean;
    }
}
于 2020-12-04T18:09:50.820 回答