问题描述
已连接到azure-cosmosdb,并能够触发默认查询,例如 findAll()
和 findById(String Id)
.但是我无法使用 @Query
注释编写本机查询,因为代码未考虑它.始终在存储库类/接口中考虑函数的名称.我需要一种将自定义查询或本机查询激发到azure-cosmos db的方法.?!
Connected to azure-cosmosdb and able to fire default queries like findAll()
and findById(String Id)
. But I can't write a native query using @Query
annotation as the code is not considering it. Always considering the name of the function in respository class/interface. I need a way to fire a custom or native query to azure-cosmos db. ?!
尝试使用@Query注释.但不起作用.
Tried with @Query annotation. But not working.
List<MonitoringSessions> findBySessionID(@Param("sessionID") String sessionID);
@Query(nativeQuery = true, value = "SELECT * FROM MonitoringSessions M WHERE M.sessionID like :sessionID")
List<MonitoringSessions> findSessions(@Param("sessionID") String sessionID);
findBySessionID()
正常运行. findSessions()
无法正常工作.运行代码时出现以下根本错误.
findBySessionID()
is working as expected. findSessions()
is not working. Below root error came while running the code.
由以下原因引起: org.springframework.data.mapping.PropertyReferenceException:未找到类型为MonitoringSessions的属性findSessions
推荐答案
感谢您的答复.我从下面的链接中得到了我真正想要的东西.归功于链接页面的作者.
Thanks for the response. I got what I exactly wanted from the below link. Credit goes to Author of the link page.
https://cosmosdb.github.io/labs/java/technical_deep_dive/03-querying_the_database_using_sql.html
公共课程计划{
private final ExecutorService executorService;
private final Scheduler scheduler;
private AsyncDocumentClient client;
private final String databaseName = "UniversityDatabase";
private final String collectionId = "StudentCollection";
private int numberOfDocuments;
public Program() {
// public constructor
executorService = Executors.newFixedThreadPool(100);
scheduler = Schedulers.from(executorService);
client = new AsyncDocumentClient.Builder().withServiceEndpoint("uri")
.withMasterKeyOrResourceToken("key")
.withConnectionPolicy(ConnectionPolicy.GetDefault()).withConsistencyLevel(ConsistencyLevel.Eventual)
.build();
}
public static void main(String[] args) throws InterruptedException, JSONException {
FeedOptions options = new FeedOptions();
// as this is a multi collection enable cross partition query
options.setEnableCrossPartitionQuery(true);
// note that setMaxItemCount sets the number of items to return in a single page
// result
options.setMaxItemCount(5);
String sql = "SELECT TOP 5 s.studentAlias FROM coll s WHERE s.enrollmentYear = 2018 ORDER BY s.studentAlias";
Program p = new Program();
Observable<FeedResponse<Document>> documentQueryObservable = p.client
.queryDocuments("dbs/" + p.databaseName + "/colls/" + p.collectionId, sql, options);
// observable to an iterator
Iterator<FeedResponse<Document>> it = documentQueryObservable.toBlocking().getIterator();
while (it.hasNext()) {
FeedResponse<Document> page = it.next();
List<Document> results = page.getResults();
// here we iterate over all the items in the page result
for (Object doc : results) {
System.out.println(doc);
}
}
}
}
这篇关于在向azure-cosmosdb发出查询时,有什么方法可以在Java JPA(DocumentDbRepository)中编写自定义查询或本机查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!