java - 无法从具有时间戳字段的 datastax 中检索数据

标签 java spring-boot cassandra datastax datastax-java-driver

我在键空间 myks 中有下表

CREATE table IF NOT EXISTS myks.users (
    user_name text,
    email text,
    created_at timestamp,
    updated_at timestamp,
    PRIMARY KEY (user_name)
);

下面是模型类
@Table(value = "users")
public @Data class Users{
    @PrimaryKey
    @Column("user_name")
    @CassandraType(type = DataType.Name.TEXT)
    private String user_name;

    @Column("email")
    @CassandraType(type = DataType.Name.TEXT)
    private String email;

    @Column("created_at")
    @CassandraType(type = DataType.Name.TIMESTAMP)
    private Timestamp created_at;

    @Column("updated_at")
    @CassandraType(type = DataType.Name.TIMESTAMP)
    private Timestamp updated_at;
}

存储库接口(interface)
@Repository
public interface UsersRepository extends CrudRepository<Users, String> {
}

将以下值插入表中
Users users = new Users();
LocalDateTime ldt_created = LocalDateTime.now();
LocalDateTime ldt_updated = ldt_created.plus(1000, ChronoUnit.MILLIS);
Timestamp ts_created = Timestamp.valueOf(ldt_created);
Timestamp ts_updated = Timestamp.valueOf(ldt_updated);
users.setUser_name(krishna");
users.setEmail("krishna@gmail.com");
users.setCreated_at(ts_created);
users.setUpdated_at(ts_updated);
usersRepository.save(users);

它已保存在表中,但在检索数据时抛出以下异常
No converter found capable of converting from type [java.util.Date] to type [java.sql.Timestamp]

最佳答案

CQL 的 timestamp类型映射到 java.util.Date Java类型,所以你需要使用它而不是Timestamp .见 CQL to Java mapping table对于这种和其他类型。

您还可以使用所谓的可选编解码器(附加依赖项)来映射 timestamp转换为其他 Java 类型,例如 Instant .见 documentation了解更多信息。

关于java - 无法从具有时间戳字段的 datastax 中检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56968855/

相关文章:

java - 为什么在父方法中返回子类名?

java - 从 native 查询返回单个投影

java - Wicket 页面销毁事件

mongodb - 在带有 MongoDB 的 Spring Boot M7 中找不到 ReflectKotlinClass

Spring 启动自动配置。意外的行为

java - Spring Boot Data JPA创建动态数据源

java - Nutch - 尝试爬行时出现错误 : JAVA_HOME is not set.

java - Criteria.uniqueResult 返回 null,但没有抛出错误

cassandra - DevCenter 报告语法错误,但在 cqlsh 上运行相同的 CQL

caching - Spark 缓存是否会以任何时间间隔自动更新数据的新值?