Search code examples
javaspring-datacouchbasesql++spring-data-couchbase

Spring data custom N1QL query returns property document to null


I'm using spring-data-couchbase 2.1.2, I redefined a repository with a custom query:

@Override
public List<PortfolioDocument> searchPortfolio() {

    CouchbaseOperations template = templateProvider.resolve(PortfolioRepository.class, PortfolioDocument.class); 

    String statement = "select META(ipdb).id AS _ID, META(ipdb).cas AS _CAS, * " + 
        "from ipdb where _class = 'com.ipdb.datamodel.document.PortfolioDocument' AND  title = 'dummytitle'";
    SimpleN1qlQuery query = N1qlQuery.simple(statement);;
    List<PortfolioDocument> portfolioDocuments = template.findByN1QL(query, PortfolioDocument.class);

    ....
}

template.findByN1QL(query, PortfolioDocument.class); returns some PortfolioDocument with all property to null except the id property. If I call template.findByOne(id); the resulting object is ok. Can you help me, please?


Solution

  • I have found a solution. The correct query is:

    SELECT META(`ipdb`).id AS _ID, META(`ipdb`).cas AS _CAS, `ipdb`.* FROM `ipdb` WHERE ...
    

    The best practice is to use the methods of N1qlUtils, you can look at the example below:

    Statement statement = N1qlUtils.createSelectClauseForEntity(template.getCouchbaseBucket().name()).
      from(Expression.i(template.getCouchbaseBucket().name()))
        .where(".....");
    

    createSelectClauseForEntity and from methods build the a part of query.