我在键区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;
}
存储库界面
@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]
答案 0 :(得分:2)
CQL的java.util.Date
类型被映射为Timestamp
Java类型,因此您需要使用它而不是timestamp
。有关此类型和其他类型,请参见CQL to Java mapping table。
您还可以使用所谓的可选编解码器(附加依赖项)将Instant
映射到其他Java类型,例如BaseModel base1 = new BaseModel();
BaseModel base2 = new BaseModel();
base1.treeSettings.addChild(base2);
base2.treeSettings.addChild(base1);
。有关更多信息,请参见documentation。