CodecNotFoundException:找不到请求操作的编解码器:[date <-> java.util.Date]

CodecNotFoundException: Codec not found for requested operation: [date <-> java.util.Date]

我使用以下 datastax 版本 java8

<dependency>
      <groupId>com.datastax.cassandra</groupId>
      <artifactId>cassandra-driver-core</artifactId>
      <version>3.7.2</version>
    </dependency>

    <dependency>
      <groupId>com.datastax.cassandra</groupId>
      <artifactId>cassandra-driver-mapping</artifactId>
      <version>3.7.2</version> 
    </dependency>

我的 table 有如下日期列

cass_table (                                                    
    data_source_id int,                                                                                                                                                                   
    company_id text,                                                              
    create_date date)  

当我尝试将数据保存到 C* table 中时,如下所示

final IndustryCompany four = new IndustryCompany(1,1236, ProdUtils.today());
 industryCompanyRepository.save(one);


public static Date today() {
        return java.sql.Date.valueOf(new SimpleDateFormat(ProducerConstants.DATABASE_DATE_FORMAT).format(Calendar.getInstance().getTime()));
    }

获取错误:

Caused by: com.datastax.driver.core.exceptions.CodecNotFoundException: Codec not found for requested operation: [date <-> java.sql.Date]
    at com.datastax.driver.core.exceptions.CodecNotFoundException.copy(CodecNotFoundException.java:57) ~[cassandra-driver-core-3.7.2.jar:na]

我在做什么错了? 如何解决这个问题?请帮忙...

您需要传递 com.datastax.driver.core.LocalDate 类型的变量。见 documentation for mapping between Java type and CQL types. You can use LocalDate from JDK 8, or from Joda time package if you'll use so-called optional codec.

另一种可能是writedate<->java.sql.Date注册直接映射

非常感谢@Alex Ott 按照你的建议,我在下面做了

添加了另一个 jar

 <dependency>
        <groupId>com.datastax.cassandra</groupId>
        <artifactId>cassandra-driver-extras</artifactId>
        <version>3.7.2</version>
    </dependency>

在调用 save() 方法之前我调用了这个

public void registerCodecs() {
    CodecRegistry registry = this.session.getCluster().getConfiguration().getCodecRegistry();
    registry.register(LocalDateCodec.instance);
}