问题描述
我有一个springboot项目,其中定义了Cassandra @Table
并使用CassandraRepository
进行操作。
我需要在主键中的一个字段中按多个值搜索,因此我无法使用任何存储库提供的查找器方法。
因此,我使用@Query
子句和IN
自变量创建了一个自定义java.util.List
方法。但是方法不获取任何数据。我没有任何异常,只是没有数据来。
这是示例代码进行解释
@Table("some_table)
Class SomeTable{
@PrimaryKey
Mykey myKey;
@Column("someColumn")
private String someColumn;
}
@PrimaryKeyClass
public class Mykey{
@PrimaryKeyColumn(name = "col_1",type = PrimaryKeyType.PARTITIONED)
private String col_1;
@PrimaryKeyColumn(name = "col_2",type = PrimaryKeyType.CLUSTERED,ordinal = 0)
private String col2;
@PrimaryKeyColumn(name = "some_date",ordinal = 1)
private LocalDate somedATE;
}
@Repository
public interface SomeTableRepo extends CassandraRepository<SomeTable,MyKey> {
@Query("Select * from some_table where col_1 in (?0) and col_2 = ?1 and some_date >= ?2 and some_date <= ?3")
List<SomeTable> findAllBySomething(List<String> col1,String col2,LocalDate startDate,LocalDate endDate);
}
如果我在cassandra Workbench上编写查询,它将为我提供数据
SELECT *
FROM myKeyspc.some_table
WHERE col_1 in ('abcd','etcetc' ) and col2 = 'some' and some_date < '2020-11-01' and some_date > '2020-01-01'
为确认是否还有其他问题,我将@Query
中SomeTableRepo
中的值硬编码,然后获取数据。例如,下面的作品。
@Repository
public interface SomeTableRepo extends CassandraRepository<SomeTable,MyKey> {
@Query("Select * from some_table where col_1 in ('abcd','etcetc') and col_2 = ?1 and some_date >= ?2 and some_date <= ?3")
List<SomeTable> findAllBySomething(List<String> col1,LocalDate endDate);
所以它对IN的明显支持不起作用,我做错了吗? 那么在CassandraRepo中不可能,那么有什么更好的方法呢?我不想多次致电findById吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)