Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Expand
titleQuery Methods


Code Block
languagejava
titleRepo with query method
@Repository
public interface AlertRepository extends CosmosRepository<Alert, String> {
// repo for alert
	List<Alert> findById(String priority);
}


Code Block
languagejava
titleController
@GetMapping("find/{id}")
    public Alert findAlert(@PathVariable String id) throws NotFoundException {
        return alertRepository.findById(id).orElseThrow(
                () -> new NotFoundException("No alert found with id: " + id)
        );
}


Query Methods with @Query annotation 


Expand
titleQuery Methods


Code Block
languagejava
titleRepository with @query methods
@Repository
public interface AddressRepository extends CosmosRepository<Address, String> {
    void deleteByPostalCodeAndCity(String postalCode, String city);

    void deleteByCity(String city);

    Iterable<Address> findByPostalCodeAndCity(String postalCode, String city);

    Iterable<Address> findByCity(String city);

    Iterable<Address> findByCityIn(List<String> cities);

    Iterable<Address> findByPostalCode(String postalCode);

    Iterable<Address> findByPostalCodeInAndCity(List<String> postalCodes, String city);

    Iterable<Address> findByStreetOrCity(String street, String city);

    @Query("select * from a where a.city = @city")
    List<Address> annotatedFindListByCity(@Param("city") String city);

    @Query("select * from a where a.city = @city")
    Page<Address> annotatedFindByCity(@Param("city") String city, Pageable pageable);
}


Code Block
languagejava
public void testAnnotatedQuery() {
        addressRepository.saveAll(Arrays.asList(Address.TEST_ADDRESS1_PARTITION1, Address.TEST_ADDRESS1_PARTITION2));

        final List<Address> result = addressRepository.annotatedFindListByCity(Address.TEST_ADDRESS1_PARTITION1.getCity());
        assertThat(result).isNotNull();
        assertThat(result.size()).isEqualTo(1);
        assertThat(result.get(0)).isEqualTo(Address.TEST_ADDRESS1_PARTITION1);
    }


Image Added


Asynchronous API

Expand
titleAll corresponding methods are represented and results are very similar to its Synchronous counterpart


Code Block
languagejava
titleAsync Client creation
import com.microsoft.azure.spring.data.cosmosdb.repository.ReactiveCosmosRepository;

import info.hayslip.AlertHoarder.models.Alert;
import org.springframework.stereotype.Repository;

@Repository
public interface AlertReactRepository extends ReactiveCosmosRepository<Alert, String> {
// repo for alert
}


...

  • Query methods are supported
  • Query methods with @Query annotation are supported
  • Better resolution for the collection names used in the crud operation 
  • Support for crud operations and query methods performed using class member such as "this."
  • Support for CosmosOperations and CosmosTemplate.

Limitations

  • Database/Collection is created as unknown, if the name is not retrieved from the properties file or if the name could not be resolved.

Future Scope

  • To Support Query Methods using @Query annotation