7

In Cassandra a column type is set to Date and in Model class type of field is set to java.util.Date with getters and setters. During com.datastax.driver.mapping.Mapper.save I get the following exception:

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)

Found the following during Google search:

DATE      <-> com.datastax.driver.core.LocalDate : use getDate()
Vijay Nandwana
  • 2,476
  • 4
  • 25
  • 42
  • Convert java.util.Date to LocalDate – Ashraful Islam Jan 05 '17 at 09:09
  • Cassandra Date field maps Java LocalDate, you need to convert Check this solution http://stackoverflow.com/a/27323328/2320144 – Ashraful Islam Jan 05 '17 at 09:13
  • to convert from Date to LocalDate, I'd need to change data type of filed in Model class to use its setter, right? – Vijay Nandwana Jan 05 '17 at 11:30
  • Yes, You need to Change your data type to LocalDate – Ashraful Islam Jan 05 '17 at 11:49
  • Actually, changing Date to LocalDate causes another issue. `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"])` – Vijay Nandwana Jan 05 '17 at 11:54

3 Answers3

7

You must convert java.util.Date to com.datastax.driver.core.LocalDate

Example:

 LocalDate localDate = LocalDate.fromMillisSinceEpoch(date.getTime());
itstata
  • 1,058
  • 7
  • 17
  • 1
    I was using the POJO mapper and I defined the `date` field as `com.datastax.driver.core.LocalDate` and it works! Thanks! – jacobcs Feb 03 '22 at 00:41
3

I was facing the same issue with using timestamp

reactor.core.Exceptions$ErrorCallbackNotImplemented: com.datastax.oss.driver.api.core.type.codec.CodecNotFoundException: Codec not found for requested operation: [TIMESTAMP <-> java.util.Date]
Caused by: com.datastax.oss.driver.api.core.type.codec.CodecNotFoundException: Codec not found for requested operation: [TIMESTAMP <-> java.util.Date]
    at com.datastax.oss.driver.internal.core.type.codec.registry.CachingCodecRegistry.createCodec(CachingCodecRegistry.java:609) ~[java-driver-core-4.9.0.jar:na]
    at com.datastax.oss.driver.internal.core.type.codec.registry.DefaultCodecRegistry$1.load(DefaultCodecRegistry.java:95) ~[java-driver-core-4.9.0.jar:na]
    at com.datastax.oss.driver.internal.core.type.codec.registry.DefaultCodecRegistry$1.load(DefaultCodecRegistry.java:92) ~[java-driver-core-4.9.0.jar:na]

Starting with driver 4.0, the CQL type timestamp is not mapped to java.util.Date anymore, but to java.time.Instant. Using Instant worked for me.

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
rajat behl
  • 151
  • 1
  • 1
  • 6
0

Replace Date type with LocalDate type in your Entity class:-

private LocalDate createdDate;
Procrastinator
  • 2,526
  • 30
  • 27
  • 36