简体   繁体   English

如何使用带有@Query 的 spring-data-couchbase 从文档中返回 boolean

[英]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.

相关问题 如何使用 spring-data-couchbase 为特定的 Couchbase 文档设置 TTL? - How to set TTL for a specific Couchbase document using spring-data-couchbase? spring-data-couchbase中的@Query与Pageable参数 - @Query with Pageable parameter in spring-data-couchbase Spring-data-couchbase-运行更新查询 - Spring-data-couchbase - running update query 我正在使用spring-data-couchbase,但是从方法名称创建查询不起作用 - I am using spring-data-couchbase , but the Query creation from method names does not work 使用 spring-data-couchbase 查询 couchbase,使用多列 - querying couchbase with spring-data-couchbase, using multiple columns 在spring-data-couchbase(N1QL)中计数查询 - Counting query in spring-data-couchbase (N1QL) Spring-Data-Couchbase-运行非临时参数化查询 - Spring-data-couchbase - running non ad-hoc parametrized query spring-data-couchbase对不存在的文档抛出DocumentDoesNotExistException - spring-data-couchbase throws DocumentDoesNotExistException for non-existent documents 使用PageRequest查找所有文档时出现spring-data-couchbase错误 - spring-data-couchbase error while finding all documents with PageRequest spring-data-couchbase - org.springframework.data.mapping.model.MappingException - spring-data-couchbase - org.springframework.data.mapping.model.MappingException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM