加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-14 19:25:53 所属栏目:Java 来源:网络整理
导读:我正在尝试从Azure Cosmos Db集合中检索文档.我遇到一个错误 Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.UnsupportedOperationException: Par

我正在尝试从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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读