猿问

必须为此操作提供 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


红颜莎娜
浏览 87回答 1
1回答

Cats萌萌

搜索了spring-data-cosmos主页中提到的分区集合findById方法的测试代码。@Test&nbsp; &nbsp; public void testFindByIdForPartitionedCollection() {&nbsp; &nbsp; &nbsp; &nbsp; final List<Address> addresses = repository.findByPostalCode(TestConstants.POSTAL_CODE);&nbsp; &nbsp; &nbsp; &nbsp; assertThat(addresses.size()).isEqualTo(2);&nbsp; &nbsp; &nbsp; &nbsp; assertThat(addresses.get(0).getPostalCode().equals(TestConstants.POSTAL_CODE));&nbsp; &nbsp; &nbsp; &nbsp; assertThat(addresses.get(1).getPostalCode().equals(TestConstants.POSTAL_CODE));&nbsp; &nbsp; }你可以在这里找到这些陈述:对于分区集合,如果要通过 findById(id) 查询记录,会抛出异常。// Incorrect for partitioned collection, exception will be thrown&nbsp; &nbsp;Address result = repository.findById(id);&nbsp; // Caution: Works for non-partitioned collection相反,您可以使用自定义查询按 ID 字段名称查询记录。// Correct, postalCode is the ID field in Address domain&nbsp; &nbsp;@Repository&nbsp; &nbsp;public interface AddressRepository extends DocumentDbRepository<Address, String> {&nbsp; &nbsp; &nbsp; List<Address> findByPostalCode(String postalCode);&nbsp; &nbsp;}&nbsp; &nbsp;// Query&nbsp; &nbsp;List<Address> result = repository.findByPostalCode(postalCode);另一种方法我发现你仍然可以在 spring-data-cosmos 包中使用 Document DB 普通 sdk,你只需要以简单的方式封装该方法。请参考此示例代码。就总结而言,这基本上是由于 Spring data commons 更改了 querylookupstrategy 正在实现的接口名称。你需要回到以前的版本cosmos-db i.e. 2.0.5!这是说明问题的链接 github.com/Microsoft/spring-data-cosmosdb/issues/304
随时随地看视频慕课网APP

相关分类

Java
我要回答