编程语言
首页 > 编程语言> > java-必须为此操作提供PartitionKey值

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

标签:azure-cosmosdb,azure-cosmosdb-sqlapi,java
来源: https://codeday.me/bug/20191211/2106366.html