【发布时间】:2017-05-19 17:27:41
【问题描述】:
在 Cassandra 中,列类型设置为 Date,而在 Model 类中,字段类型设置为 java.util.Date,并带有 getter 和 setter。在com.datastax.driver.mapping.Mapper.save 期间,我收到以下异常:
Codec not found for requested operation: [date <-> java.util.Date]
Caused by: com.datastax.driver.core.exceptions.CodecNotFoundException: Codec not found for requested operation: [date <-> java.util.Date]
at com.datastax.driver.core.exceptions.CodecNotFoundException.copy(CodecNotFoundException.java:56)
at com.datastax.driver.core.exceptions.CodecNotFoundException.copy(CodecNotFoundException.java:25)
at com.datastax.driver.mapping.DriverThrowables.propagateCause(DriverThrowables.java:41)
at com.datastax.driver.mapping.Mapper.save(Mapper.java:272)
在 Google 搜索过程中发现以下内容:
DATE <-> com.datastax.driver.core.LocalDate : use getDate()
【问题讨论】:
-
将 java.util.Date 转换为 LocalDate
-
Cassandra Date 字段映射Java LocalDate,需要转换查看这个解决方案stackoverflow.com/a/27323328/2320144
-
要从 Date 转换为 LocalDate,我需要更改 Model 类中的字段数据类型才能使用它的 setter,对吧?
-
是的,您需要将数据类型更改为 LocalDate
-
实际上,将 Date 更改为 LocalDate 会导致另一个问题。
Caused by: org.codehaus.jackson.map.JsonMappingException: Can not instantiate value of type [simple type, class java.time.LocalDate] from JSON String; no single-String constructor/factory method (through reference chain: com.test.model.User["testList"]->com.test.model.DepTest["testDob"])