java-必须为此操作提供PartitionKey值

我正在尝试从Azure Cosmos Db集合中检索文档.我遇到一个错误

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing Failed; nested exception is java.lang.UnsupportedOperationException: PartitionKey value must be supplied for this operation.] with root cause
java.lang.UnsupportedOperationException: PartitionKey value must be supplied for this operation.

我试图在网上查找如何为函数findById()提供分区键值,但似乎“ Azure Spring数据cosmosdb”没有选择为Java提供功能的分区键实作

orderTransactionRepository.findById("id").get
最佳答案
搜索了071000的主页中提到的用于分区集合的findById方法test code.

@Test
    public void testFindByIdForPartitionedCollection() {
        final List<Address> addresses = repository.findByPostalCode(TestConstants.POSTAL_CODE);

        assertthat(addresses.size()).isEqualTo(2);
        assertthat(addresses.get(0).getPostalCode().equals(TestConstants.POSTAL_CODE));
        assertthat(addresses.get(1).getPostalCode().equals(TestConstants.POSTAL_CODE));
    }

您可以找到语句here

对于分区集合,如果要通过findById(id)查询记录,将引发异常.

// Incorrect for partitioned collection,exception will be thrown
   Address result = repository.findById(id);  // Caution: Works for non-partitioned collection

相反,您可以使用自定义查询按ID字段名称查询记录.

// Correct,postalCode is the ID field in Address domain
   @Repository
   public interface AddressRepository extends DocumentDbRepository<Address,String> {
      List<Address> findByPostalCode(String postalCode);
   }

   // Query
   List<Address> result = repository.findByPostalCode(postalCode);

我发现另一种方法是,您仍然可以在spring-data-cosmos包中使用Document DB normal sdk,您只需要以一种简单的方式封装该方法.请参考此sample code.

仅作总结,基本上是由于Spring数据公共更改了querylookupstrategy正在实现的接口名称.您需要返回到cosmos-db的先前版本,即2.0.5!这是说明问题的链接github.com/Microsoft/spring-data-cosmosdb/issues/304

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...