我的应用程序使用 Spring Boot、JPA 和 Crnk 框架以及 mariadb 作为数据库。
我有一个这样定义的字段
@Column(name = "baskets", columnDefinition = "TEXT")
@Convert(converter = UUIDListConverter.class)
private List<UUID> baskets;
在 mariadb 它看起来像这样
我的映射器是这样写的
@Component
public class UUIDListConverter implements AttributeConverter<List<UUID>, String> {
@Autowired
private ObjectMapper objectMapper;
@Override
public List<UUID> convertToEntityAttribute(String dbData) {
try {
return objectMapper.readValue(dbData, new TypeReference<List<UUID>>() {});
} catch (JsonProcessingException e) {
throw new IllegalArgumentException(
"Unable to convert attribute JSON from database format: " + e.getMessage(), e);
}
}
}
我尝试在 mariadb 中使用以下查询更新篮子的值。我在 mariadb 的 mysql gui 工具中运行了这个查询。
update table_name set baskets = (UUID()) where code = 'XXX';
它将值插入到购物篮列中。插入列中的值是这个
a5cff67a-6098-11ec-9745-acde48001122
但是当我尝试读取列并转换为模型对象时。它给了我以下错误
org.springframework.orm.jpa.JpaSystemException: Error attempting to apply AttributeConverter; nested exception is javax.persistence.PersistenceException: Error attempting to apply AttributeConverter
Caused by: java.lang.IllegalArgumentException: Unable to convert attribute JSON from database format: Unrecognized token 'a5cff67a': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')
at [Source: (String)"a5cff67a-6098-11ec-9745-acde48001122"; line: 1, column: 9]
at util.UUIDListConverter.convertToEntityAttribute(UUIDListConverter.java:34)
at util.UUIDListConverter.convertToEntityAttribute(UUIDListConverter.java:12)
at org.hibernate.metamodel.model.convert.internal.JpaAttributeConverterImpl.toDomainValue(JpaAttributeConverterImpl.java:45)
at org.hibernate.type.descriptor.converter.AttributeConverterSqlTypeDescriptorAdapter$2.doConversion(AttributeConverterSqlTypeDescriptorAdapter.java:140)
... 148 more
Caused by: com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'a5cff67a': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')
at [Source: (String)"a5cff67a-6098-11ec-9745-acde48001122"; line: 1, column: 9]
- 是不是因为购物篮列的数据类型是“文本”。?
2.数据类型应该是“BINARY(16)”来保存UUID值吗?
任何其他建议/帮助