我怎样才能覆盖 javax.persistence.AttributeConverter
How can I override a javax.persistence.AttributeConverter
我遇到了问题,想要覆盖实体子类中的 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
{
...
@Convert 似乎不适合覆盖超类字段的现有转换器。我以不同的方式解决了它。我通过 CDI 将一个 conversionService 注入到超类的 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;
}
}
我遇到了问题,想要覆盖实体子类中的 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
{
...
@Convert 似乎不适合覆盖超类字段的现有转换器。我以不同的方式解决了它。我通过 CDI 将一个 conversionService 注入到超类的 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;
}
}