microsoft / spring-data-cosmosdb

Access data with Azure Cosmos DB
MIT License
94 stars 64 forks source link

how to write native sql in @Repository #385

Open tapanp opened 5 years ago

tapanp commented 5 years ago

I am using "azure-cosmosdb-spring-boot-starter" to talk to cosmos DB. Normal CrudRepository queries (findAll, findByColumnName etc) works fine, I am getting following exception while executing native query: com.microsoft.azure.spring.data.cosmosdb.exception.DocumentDBAccessException: Failed to execute find operation from student_assessment_summary; nested exception is java.lang.IllegalStateException: com.microsoft.azure.documentdb.DocumentClientException: Cross partition query is required but disabled. Please set x-ms-documentdb-query-enablecrosspartition to true, specify x-ms-documentdb-partitionkey, or revise your query to avoid this exception. ActivityId: 1ea51810-5259-4339-b3ca-0f57e3a0d086, Microsoft.Azure.Documents.Common/2.4.0.0, StatusCode: BadRequest at com.microsoft.azure.spring.data.cosmosdb.core.DocumentDbTemplate.find(DocumentDbTemplate.java:408) at com.microsoft.azure.spring.data.cosmosdb.repository.query.DocumentDbQueryExecution$MultiEntityExecution.execute(DocumentDbQueryExecution.java:40) at com.microsoft.azure.spring.data.cosmosdb.repository.query.AbstractDocumentDbQuery.execute(AbstractDocumentDbQuery.java:31) at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:605)

My repository is declared as:

`
@Repository public interface AssessmentSummaryCosmosRepository extends DocumentDbRepository<AssessmentSummaryCosmos, String> {

static final String SUMMARY_BY_STUDENTS = "SELECT * FROM assessment_summary c where c.name in :name and c.assessmentId = :assessmentId";

@Query(value = SUMMARY_BY_STUDENTS, nativeQuery = true) List findAllByStudentSRNAndAssessmentId(@Param("name") List name, @Param("assessmentId") Long assessmentId); }`

Incarnation-p-lee commented 5 years ago

@tapanpDoe Looks the you mean Query annotation but this is not supported yet. I will add it to our feature list.

DaireJN commented 5 years ago

+1 for this feature.

sombhattacharyya1983 commented 3 years ago

Is there a suggestion on how to do a custom query this for users of this version of the library ?

kushagraThapar commented 3 years ago

@sombhattacharyya1983 - you can retrieve the cosmosClient through applicationContext, and use the cosmosClient to execute custom queries. (through the native Cosmos Java SDK)

sombhattacharyya1983 commented 3 years ago

@sombhattacharyya1983 - you can retrieve the cosmosClient through applicationContext, and use the cosmosClient to execute custom queries. (through the native Cosmos Java SDK)

Thank you. Yes I looked at the test files to understand the code. Unfortunately , we are working on a Spring boot 2.1.x project and I have to continue to use the older version(2.1.2.FIX1) of this library. So I fell back on using DocumentClient . Appreciate your quick reply.