java - Cassandra Spring Data不允许我直接通过复合主键查询数据
问题描述
我正在尝试使用复合主键类来使用 Spring Data Cassandra。但是当我尝试查询数据时出现异常:
org.springframework.data.repository.query.QueryCreationException: **Could not create query for public abstract java.util.List com.amdocs.cassandrapoc.repository.ServiceRepository.findByKey(com.amdocs.cassandrapoc.entities.Key)! Reason: Cannot use composite primary key directly. Reference a property of the composite primary key**
这是我的代码:
@PrimaryKeyClass
public class Key implements Serializable {
@PrimaryKeyColumn(name = "id", ordinal = 0, type =
PrimaryKeyType.PARTITIONED)
@CassandraType(type = DataType.Name.UUID)
private UUID id;
@PrimaryKeyColumn(name = "tenant", ordinal = 1, type =
PrimaryKeyType.PARTITIONED)
@CassandraType(type = DataType.Name.TEXT)
private String tenant;
(Equals, hashcode, getters, setters omitted)
}
Table(value = "service")
public class Service implements Serializable {
@PrimaryKey
private Key key;
@Column
private String value;
(constructors, setters, getters omitted)
}
@Repository
public interface ServiceRepository extends CassandraRepository<Service, Key> {
List<Service> findByKey(Key key);
}
这就是我创建表的方式(使用嵌入式 Cassandra 和 junit):
@Autowired
private CassandraAdminOperations adminTemplate;
private final String DATA_TABLE_NAME = "service";
@Before
public void createTable() {
adminTemplate.createTable(
true, CqlIdentifier.of(DATA_TABLE_NAME),
Service.class, new HashMap<String, Object>());
}
我做错了什么?
解决方案
我面临同样的问题。在我的情况下,我通过以下实现解决了它。
@Repository
public interface ServiceRepository extends CassandraRepository<Service, Key> {
List<Service> findByKeyIdAndKeyTenant(UUID id, String tenant);
}
推荐阅读
- node.js - 我在 ionic 中使用 tesseract.js 从图像中提取文本。现在有没有办法在键值对中表示提取的文本?
- excel - 将字符串添加到内容控件框/将下拉列表替换为内容控件
- r - 在R中分配向量的一半
- ansible - Ansible 剧本 - 什么是剧本?
- python - 将文件从 lambda 推送到 s3
- android - 应用程序关闭后,用于媒体控件的 Android 广播接收器停止工作
- java - Hackerrank:夏洛克和字谜
- graphql - 如何将密钥与自定义解析器与放大或 appsync graphql 查询相结合?
- node.js - AWS - 不接收 HTTPS 请求
- ios - 具有双向无限水平滚动和分页功能的 CollectionView