简体   繁体   English

找不到请求的操作的编解码器:[duration <-> java.time.Duration]

[英]Codec not found for requested operation: [duration <-> java.time.Duration]

having the below table 有下表

  "CREATE TABLE IF NOT EXISTS user_preferences (" +
                        "    user_id text," +
                        "    my_duration duration," +
                        "    last_modified timestamp," +
                        "    primary key((id))" +
                        ");";

when trying to persist the below model 尝试保留以下模型时

import com.datastax.driver.mapping.annotations.Column;
import com.datastax.driver.mapping.annotations.PartitionKey;
import com.datastax.driver.mapping.annotations.Table;
import java.time.Duration;

@Table(name = "user_preferences")
public class UserPreferences {

    @PartitionKey
    @Column(name = "user_id")
    private String userId;

    @Column(name = "my_duration")
    private Duration myDuration;

    @Column(name = "last_modified")
    private Date lastModified;
}

i get this codec not found exception. 我得到此编解码器未找到异常。

com.datastax.driver.core.exceptions.CodecNotFoundException: Codec not found for requested operation: [duration <-> java.time.Duration]
    at com.datastax.driver.core.exceptions.CodecNotFoundException.copy(CodecNotFoundException.java:57) ~[cassandra-driver-core-3.6.0.jar:na]
    at com.datastax.driver.core.exceptions.CodecNotFoundException.copy(CodecNotFoundException.java:25) ~[cassandra-driver-core-3.6.0.jar:na]
    at com.datastax.driver.mapping.DriverThrowables.propagateCause(DriverThrowables.java:39) ~[cassandra-driver-mapping-3.6.0.jar:na]
    at com.datastax.driver.mapping.Mapper.save(Mapper.java:356) ~[cassandra-driver-mapping-3.6.0.jar:na]

NOTE: reading works fine probably because the table is not populated yet. 注意:读取工作正常,可能是因为尚未填充表格。

is java.time.Duration supported by Datastax-core 3.3.2 ? Datastax-core 3.3.2支持java.time.Duration吗?


Addition from comments: Default codec returns com.datastax.driver.core.Duration! 注释中的补充:默认编解码器返回com.datastax.driver.core.Duration! C* duration is not compatible with java.time.Duration. C *持续时间与java.time.Duration不兼容。 So you should use driver Duration type in you code or provide you own codec. 因此,您应该在代码中使用驱动程序Duration类型,或者提供自己的编解码器。

Wrong answer for this particular question but still useful if you are goint to implement your own codec, you will have to register it that way. 这个特定问题的答案错误,但是如果您想实施自己的编解码器仍然很有用,则必须以这种方式进行注册。

For other java.time classes you need to use/register extra jdk8 codecs: https://docs.datastax.com/en/developer/java-driver/3.1/manual/custom_codecs/extras/ 对于其他java.time类,您需要使用/注册额外的jdk8编解码器: https ://docs.datastax.com/en/developer/java-driver/3.1/manual/custom_codecs/extras/

import com.datastax.driver.extras.codecs.jdk8.InstantCodec;
import java.time.Instant;

cluster.getConfiguration().getCodecRegistry()
    .register(InstantCodec.instance);

or if you have no access to cluster object 或者如果您无权访问群集对象

com.datastax.driver.core.CodecRegistry.DEFAULT_INSTANCE.register(InstantCodec.instance);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM