Spring data custom N1QL query returns property document to null

java
n1ql

#1

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

@Override
public List 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 calltemplate.findByOne(id); the resulting object is ok.
Can you help me, please? Bye.


#2

Have you tried with a “query derivation”? That is, don’t do a custom implementation but rather just add the following method signature to your repository’s interface:

public List<PortfolioDocument> findByTitleEquals(String title);

If the above works, then it’s probably an issue with you N1QL statement… although from a first look nothing particularly wrong stands out.


#3

I have found a solution. The correct query is:

SELECT META(ipdb).id AS _ID, META(ipdb).cas AS _CAS,ipdb.* FROMipdbWHERE ...
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.

Bye,


#4

Ah, so just missing the ipdb. before the * :slight_smile:
Note my “query derivation” suggestion above does use this utility class to generate the same query for you :wink:
Glad you found the solution!


#5

Thanks but the where criteria is more complex, this is just a example. Bye.