Spring Data Couchbase: Count() nor findAll() does not work but findBy() works

Viewed 244

I am currently setting up a Rest API server using Spring Boot (v2.5.5), Spring Data Couchbase (v4.2.5) and Couchbase (v6.6.1).

I get a really strange behavior when requesting

  • count() -> 0
  • findAll() -> []

Whereas

  • findById() is returning a result.

My entity:

{"mkey": { "keyContent": "AA", "mkeyStatus": "L" }, "sequences": [ { "direction": "B", "loc1Value": "NCE", "loc2Value": "NYC" } ] }

@Document @Data @AllArgsConstructor @NoArgsConstructor @EqualsAndHashCode public class AirlineProfile {

    @Id private String id;

    @Field private MKey mkey;

    @Field private List<Sequence> sequences;

    @EqualsAndHashCode @AllArgsConstructor @NoArgsConstructor @Data static class MKey {

        @Field private String keyContent;

        @Field private String mkeyStatus;
    }

    @EqualsAndHashCode @AllArgsConstructor @NoArgsConstructor @Data static class Sequence {

        @Field private String loc1Value;

        @Field private String loc2Value;

        @Field private String direction;
    }
}

My repository is extending the CrudRepository.

public interface AirlineProfileRepository extends CrudRepository<AirlineProfile, String> {}

While my Service is the following:

@Service @Qualifier("AirlineProfileServiceImpl") public class AirlineProfileServiceImpl
        implements AirlineProfileService {

    @Autowired private AirlineProfileRepository airlineProfileRepository;

    @Override
    public long count() {
        return airlineProfileRepository.count();
    }

    @Override
    public List<AirlineProfile> findAll() {
        List<AirlineProfile> airlineProfiles = new ArrayList<>();
        for (AirlineProfile airlineProfile : airlineProfileRepository.findAll()) {
            airlineProfiles.add(airlineProfile);
        }
        return airlineProfiles;
    }

    @Override public AirlineProfile findById(String id) {
        return airlineProfileRepository.findById(id).orElse(null);
    }
}

And my controller the following:

@RestController @RequestMapping("/api") public class AirlineProfileController {

    @Autowired AirlineProfileService airlineProfileService;

    @GetMapping("/airlineprofile/count") public long count() {
        System.out.println("Count");
        return airlineProfileService.count();
    }

    @GetMapping("/airlineprofile/all") public List<AirlineProfile> getAllAirlineProfiles() {
        System.out.println("Get all AirlineProfile");
        return airlineProfileService.findAll();
    }

    @GetMapping("/airlineprofile/id={id}") public AirlineProfile getAirlineProfileById(@PathVariable String id) {
        System.out.println("Get AirlineProfile for id = " + id);
        return airlineProfileService.findById(id);
    }
}

I do not know if I missed something at Server or Couchbase side ... :(

Thank you for your help!

1 Answers

Ok, found that:

public interface AirlineProfileRepository extends CrudRepository<AirlineProfile, String> {

    @Query("#{#n1ql.selectEntity}")
    List<AirlineProfile> findAll();
}

Is working ...

So, I am questioning myself about the usability of findAll() ...

Related