按主键查找所有

Find all by primary key

我有一个卡桑德拉table:

pk key value
pk_1 key_1 value_1
pk_1 key_2 value_2
pk_1 key_3 value_3

其中pkkey组成复合键(pk是分区键,key是集群)。

我想通过 pk 使用 Spring JPA 检索所有 key-value 对,得到 Map<String, String> 作为结果。

我试过以下方法:

public interface MyRepository extends CassandraRepository<MyEntity, MyEntityCompoundKey> {
    @Query("SELECT key, value FROM my_table WHERE pk='?0'")
    Map<String, String> getAll(String pk);

    // other crud methods
}

但是,当使用 pk_1 作为参数调用它时,出现以下错误:

org.springframework.dao.IncorrectResultSizeDataAccessException: Incorrect result size: expected 1, actual 3

我该如何解决?有没有更好的方法来仅通过主键检索元素?

编辑

@Table
public class MyEntity {

    @PrimaryKey
    pribate MyEntityCompoundKey key;
    
    @CassandraType(type = Name.Text)
    private String value;
}

@PrimaryKeyClass
public class MyEntityCompoundKey {
  
    @PrimaryKeyColumn(name = "pk", type= PARTITIONED)
    private String pk;

    @PrimaryKeyColumn(name = "key", type = CLUSTERED)
    private String key;
}

您可以在您的存储库中编写一个带有签名的方法

public interface MyRepository extends CassandraRepository<MyEntity, MyEntityCompoundKey> {
    List<MyEntity> findByKeyPk(String pk)
}
    

这将由 spring 自动实现并做你想做的事。