Spring 带列表参数的数据库 N1QL
Spring data couchbase N1QL with list parameter
我想检索包含参数中至少一个给定标签的第一个对象。
@Query("#{#n1ql.selectEntity} WHERE SOME t IN tags SATISFIES t IN [\"#{#currentTags}\"] " +
"END AND #{#n1ql.filter} LIMIT 1")
RecipeEntry findFirstContainingTags(@Param("currentTags")List<String> tags);
但是生成的 N1QL 查询是
"SELECT * FROM contents WHERE SOME t IN tags SATISFIES t IN ["tag1,tag2"] END LIMIT 1"
预期的查询是
"SELECT * FROM contents WHERE SOME t IN tags SATISFIES t IN ["tag1","tag2"] END LIMIT 1"
所以我希望我的参数 currentTags 将转换为 ["tag1","tag2"] 而不是 ["tag1,tag2"]
是否可以使用 Spring 数据生成此查询?
有关信息,请参阅我所做的以下内容:
- 我使用 jackson objectMapper 将我的字符串列表转换为 json
- 将json作为参数
服务:
public RecipeEntry findMostRecentWithAtLeastTags(List<String> tags) {
RecipeEntry recipeEntry = null;
try {
String tagsArray = objectMapper.writeValueAsString(tags);
recipeEntry = ((RecipeQueryRepository) this.queryRepository).findMostRecentWithAtLeastTags(tagsArray);
} catch (Exception e) {
log.error("Error while retrieving most recent recipe with given tags", e);
}
return recipeEntry;
}
存储库:
@Query("#{#n1ql.selectEntity} WHERE status = 'PUBLISHED' AND (EVERY t IN #{#currentTags} SATISFIES t IN tags END) " +
"AND #{#n1ql.filter} ORDER BY publishDate DESC LIMIT 1")
RecipeEntry findMostRecentWithAtLeastTags(@Param("currentTags") String tags);
我想检索包含参数中至少一个给定标签的第一个对象。
@Query("#{#n1ql.selectEntity} WHERE SOME t IN tags SATISFIES t IN [\"#{#currentTags}\"] " +
"END AND #{#n1ql.filter} LIMIT 1")
RecipeEntry findFirstContainingTags(@Param("currentTags")List<String> tags);
但是生成的 N1QL 查询是
"SELECT * FROM contents WHERE SOME t IN tags SATISFIES t IN ["tag1,tag2"] END LIMIT 1"
预期的查询是
"SELECT * FROM contents WHERE SOME t IN tags SATISFIES t IN ["tag1","tag2"] END LIMIT 1"
所以我希望我的参数 currentTags 将转换为 ["tag1","tag2"] 而不是 ["tag1,tag2"]
是否可以使用 Spring 数据生成此查询?
有关信息,请参阅我所做的以下内容:
- 我使用 jackson objectMapper 将我的字符串列表转换为 json
- 将json作为参数
服务:
public RecipeEntry findMostRecentWithAtLeastTags(List<String> tags) {
RecipeEntry recipeEntry = null;
try {
String tagsArray = objectMapper.writeValueAsString(tags);
recipeEntry = ((RecipeQueryRepository) this.queryRepository).findMostRecentWithAtLeastTags(tagsArray);
} catch (Exception e) {
log.error("Error while retrieving most recent recipe with given tags", e);
}
return recipeEntry;
}
存储库:
@Query("#{#n1ql.selectEntity} WHERE status = 'PUBLISHED' AND (EVERY t IN #{#currentTags} SATISFIES t IN tags END) " +
"AND #{#n1ql.filter} ORDER BY publishDate DESC LIMIT 1")
RecipeEntry findMostRecentWithAtLeastTags(@Param("currentTags") String tags);