spring-data-cosmosdb icon indicating copy to clipboard operation
spring-data-cosmosdb copied to clipboard

how to write native sql in @Repository

Open tapanp opened this issue 5 years ago • 5 comments

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<AssessmentSummaryCosmos> findAllByStudentSRNAndAssessmentId(@Param("name") List<String> name, @Param("assessmentId") Long assessmentId); }`

tapanp avatar Jul 01 '19 02:07 tapanp

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

Incarnation-p-lee avatar Jul 01 '19 02:07 Incarnation-p-lee

+1 for this feature.

DaireJN avatar Jul 05 '19 11:07 DaireJN

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

sombhattacharyya1983 avatar Feb 10 '21 15:02 sombhattacharyya1983

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

kushagraThapar avatar Feb 11 '21 19:02 kushagraThapar

@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.

sombhattacharyya1983 avatar Feb 12 '21 04:02 sombhattacharyya1983