我的应用程序使用Spring、JPA和Crnk Framework以及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工具中运行了这个查询。
update table_name set baskets = (UUID()) where code = 'XXX';
它将值插入到basket列中。插入到列中的值是
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.数据类型是否应该是“二进制(16)”以保存UUID值。
任何其他建议/帮助
发布于 2021-12-19 14:37:21
1.
是因为篮列的数据类型是"Text“。?
我怀疑是这样!
我认为:
objectMapper.readValue(dbData, new TypeReference<List<UUID>>() {})
..throws异常,因为:
a5cff67a-6098-11ec-9745-acde48001122
,没有引号,没有大括号,只是普通的uuid)看起来不像有效json!update table_name set baskets = ('["'||UUID()||'"]') where code = 'XXX';
,试图产生有效的json),objectMapper应该调用哪个转换器!??!;) (-> 堆栈过流!)2.
数据类型是否应该是“二进制(16)”以保存UUID值。?
娜阿!
最好的,大概:JSON
任何其他建议/帮助
https://stackoverflow.com/questions/70409636
复制相似问题