如何将Object转换为转换器中的特定参数?

时间:2014-12-05 11:52:42

标签: java jpa casting

我有一个像这个例子的多个JPA转换器:

@Converter
public class CurrencyConverter4jpa
implements AttributeConverter<Currency, String> {
    @Override
    public String convertToDatabaseColumn( Currency value ) {
        if ( value == null )
            return null;
        return value.getCurrencyCode();
    }
    @Override
    public Currency convertToEntityAttribute( String value ) {
        if ( value == null )
            return null;
        Currency cur = Currency.getInstance( value );
        return cur;
    }
}

另一个是将UUID转换为'二进制(16)'并返回。我将实例化的转换器存储在一个字段中:

private Class<?> convClass = CurrencyConverter.class;
AttributeConverter<?,?> conv = converterClass.newInstance();
public void setConverter( Class<?> converterClass ) {
    this.convClass = converterClass;
    this.conv = converterClass.newInstance();
}

public Object getSQLField( ResultSet rs ) {
    Object value = null;
    if ( conv != null ) {
        value = conv.convertToEntityAttribute( resultSet.getObject( 4 ));
    ...

仅将此转换器与Java对象一起使用会导致编译器错误。我尝试过的是:

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

.convertToEntityAttribute的参数类型应为String,您要发送Object。尝试将resultSet.getObject替换为resultSet.getString