gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:06:15 26 4
gpt4 key购买 nike

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

最佳答案

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

@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 :

对于Partitioned collection,如果想通过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 正在实现的接口(interface)名称。你需要回到以前版本的 cosmos-db 即 2.0.5!这是说明问题的链接 github.com/Microsoft/spring-data-cosmosdb/issues/304

关于java - 必须为此操作提供 PartitionKey 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54640748/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com