PartitionKey value must be supplied for this operation

◇◆丶佛笑我妖孽 提交于 2019-12-23 17:17:56

问题


I am trying to retrieve a document from the Azure Cosmos Db Collection. I am running into an error

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.

I was trying to look up online how can I provide the partition key value to the function findById(), but it seems like the "Azure Spring data cosmosdb" doesn't have the option to provide the partition key with the function for the java implementation

orderTransactionRepository.findById("id").get

回答1:


Searched for the test code of findById method for partitioned collection which is mentioned in the home page of 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));
    }

You could find the statements here:

For Partitioned collection, if you want to query records by findById(id), exception will be thrown.

// Incorrect for partitioned collection, exception will be thrown
   Address result = repository.findById(id);  // Caution: Works for non-partitioned collection

Instead, you can query records by ID field name with custom query.

// 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);

Another way i found that you could still could use Document DB normal sdk in the spring-data-cosmos package,you just need to encapsulate the method in a simple way. Please refer to this sample code.


Just for summarized , it was basically due to Spring data commons changing the interface name that the querylookupstrategy was implementing. You need go back to the previous version of the cosmos-db i.e. 2.0.5! Here is the link stating the issue github.com/Microsoft/spring-data-cosmosdb/issues/304



来源:https://stackoverflow.com/questions/54640748/partitionkey-value-must-be-supplied-for-this-operation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!