PartitionKey value must be supplied for this operation

Nikhil Jain :

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
Jay Gong :

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=101230&siteId=1