1

I am using 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.

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. But I rather not use couchbasetemplate directly or sub-document apis of couchbase sdk.

A similar older question How to fetch a field from document using n1ql with spring-data-couchbase

Matthew Groves
  • 25,181
  • 9
  • 71
  • 121
Sahil333
  • 63
  • 9

1 Answers1

1

A proposed solution is to define a DTO and its repository specifically for fields one wants.

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);

}
Sahil333
  • 63
  • 9