Urgent inquiry: Spring Data Couchbase N1QL based query not working

Hi Everyone,

I got problem to make the spring data couchbase N1QL based repository methods working. Need urgent support.

Thanks in advance!!! – Allen

I followed the sample(http://projects.spring.io/spring-data-couchbase/) and created my own project.

  1. Created the “all” view - findAll and count works fine
  2. Created primary index (also tried normal index)
  3. Tried return List and Page result
  4. Tried spring data auto-generated query
  5. Tried Query annotation

So far, all N1QL based queries are not working - they just return empty. I ran similar query directly (select * from poiapp where country=‘Japan’), it returns data set.

Here is my setup:

Couchbase: Community 4.5 Beta
Spring Boot: 1.4.0.M3
Spring Data Couchbase: 2.1.1.RELEASE
Java client: 2.2.7

Here is the code snippet:

@Repository
public interface PoiRepository extends CrudRepository<Poi, String> {
    // backed by an auto-generated N1QL query
    public Page<Poi> findByName(String name, Pageable pageable);
    public long countByName(String name);
    public List<Poi> findByCountry(String country);
    public Page<Poi> findByCountryAndCity(String country, String city, Pageable pageable);
    public List<Poi> findByCountryAndCity(String country, String city);
    public Page<Poi> findByCountryAndCityAndName(String Country, String city, String name, Pageable pageable);
    public Page<Poi> findAll(Pageable pageable);
    // Geo search example, backed by byLocation view
    @Dimensional(designDocument = "poiGeo", spatialViewName = "byLocation")
    public GeoPage<Poi> findByLocationNear(Point p, Distance r, Pageable pageable);
    
    @Dimensional(designDocument = "poiGeo", spatialViewName = "byLocation")
    public GeoPage<Poi> findByLocationWithin(Box cityBoundingBox, Pageable pageable);
    
    @Query("#{#n1ql.selectEntity} WHERE country = $1 AND #{#n1ql.filter}")
    public List<Poi> findAllCountry(String country);
}
public Page<Poi> getPoi(
        @RequestParam("country") String country, 
        @RequestParam("city") String city, 
        Pageable pageable) {
    System.out.println("Country: " + country);
    System.out.println("City: " + city);
    return poiRepo.findByCountryAndCity(country, city, pageable);
}

Alright, I can’t wait and trace into the code and found the problem:

The documents in the database are loaded by other tool which has no _class field. For auto-generated queries, this is always required. For Query annotation based query, seems this “AND #{#n1ql ilter}” appends that.

1 Like

One more question. We use “type” field in our document. Is there a way in to instruct the spring couchbase to use that instead of _class?

1 Like

Hi did you find the solution for the same. since i have tried https://blog.couchbase.com/couchbase-spring-boot-spring-data/ .

but i am not getting N1QLquery result always it is coming as null.
do we need to do any Couchbase database side configuration.
Can you please help for the same.