亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

必須為此操作提供 PartitionKey 值

必須為此操作提供 PartitionKey 值

紅顏莎娜 2022-06-15 17:31:46
我正在嘗試從 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 causejava.lang.UnsupportedOperationException: PartitionKey value must be supplied for this operation.我試圖在網上查找如何為函數 findById() 提供分區鍵值,但似乎“Azure Spring 數據 cosmosdb”沒有為 java 函數提供分區鍵的選項執行orderTransactionRepository.findById("id").get
查看完整描述

1 回答

?
Cats萌萌

TA貢獻1805條經驗 獲得超9個贊

搜索了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));

    }

你可以在這里找到這些陳述:


對于分區集合,如果要通過 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 普通 sdk,你只需要以簡單的方式封裝該方法。請參考此示例代碼。


就總結而言,這基本上是由于 Spring data commons 更改了 querylookupstrategy 正在實現的接口名稱。你需要回到以前的版本cosmos-db i.e. 2.0.5!這是說明問題的鏈接 github.com/Microsoft/spring-data-cosmosdb/issues/304


查看完整回答
反對 回復 2022-06-15
  • 1 回答
  • 0 關注
  • 111 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號