微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

原文地址:https://www.jb51.cc/java/532844.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐