必须为此操作提供PartitionKey值

时间:2019-02-11 23:40:09

标签: java azure-cosmosdb azure-cosmosdb-sqlapi

我正在尝试从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 data cosmosdb”没有选择为分区键提供该功能用于Java实现

orderTransactionRepository.findById("id").get

1 个答案:

答案 0 :(得分:2)

搜索test code主页中提到的用于分区集合的findById方法的spring-data-cosmos

@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 i.e. 2.0.5的先前版本!这是说明问题github.com/Microsoft/spring-data-cosmosdb/issues/304

的链接