[英]How to return a boolean from a document using spring-data-couchbase with @Query
I am using spring-data-couchbase 4.1.1.我正在使用 spring-data-couchbase 4.1.1。 I want to add a method in my repository method like following
我想在我的存储库方法中添加一个方法,如下所示
public interface DomainRepository extends CouchbaseRepository<Domain, String> {
@Query(("SELECT isActive from #{#n1ql.bucket} "
+ "WHERE META().id = " + "\"#{#id}\" "))
Boolean isActive(@Param("id") String id);
}
But it throws ClassCastException for Domain to Boolean which is acceptable I believe.但是它将域的 ClassCastException 抛出到 Boolean 我相信这是可以接受的。
Is there any other simple way to do it?还有其他简单的方法吗? I believe there was a findByN1QlProjection in older version of couchbaseTemplate, can't find it in this version though.
我相信旧版本的 couchbaseTemplate 中有一个 findByN1QlProjection,但在这个版本中找不到它。 But I rather not use couchbasetemplate directly or sub-document apis of couchbase sdk.
但我宁愿不直接使用 couchbasetemplate 或 couchbase sdk 的子文档 api。
A similar older question How to fetch a field from document using n1ql with spring-data-couchbase一个类似的旧问题How to fetch a field from document using n1ql with spring-data-couchbase
A proposed solution is to define a DTO and its repository specifically for fields one wants.一种建议的解决方案是专门为需要的字段定义 DTO 及其存储库。
class IsActiveDTO {
private isActive;
// constructor, getters, and setters
}
and define its repository并定义其存储库
public interface IsActiveDTORepository extends CouchbaseRepository<IsActiveDTO, String> {
@Query(("SELECT isActive from #{#n1ql.bucket} "
+ "WHERE META().id = " + "\"#{#id}\" "))
IsActiveDTO isActive(@Param("id") String id);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.