首页 文章

在couchbase N1Ql @query中使用IN子句或使用来自couchbase JPA的findAll(keys)

提问于
浏览
1

我正在使用Spring couchbase JPA并尝试通过提供密钥列表来获取文档 . 我的存储库结构看起来像

public interface EmployeeRepo
    extends CouchbasePagingAndSortingRepository<Employee, String>, EmployeeCustomRepo {

我有一个员工ID列表来搜索和获取相应的文档 . 我尝试使用curdRepository中的方法

public List<Employee> findAllById(List<String> empIds) {
        return employeeRepo.findAll(empIds);
    }

但我得到例外::

org.springframework.dao.InvalidDataAccessResourceUsageException: 
View employee/all does not exist.; nested exception is 
com.couchbase.client.java.error.ViewDoesNotExistException: View 
employee/all does not exist.

甚至我也尝试将我的密钥列表包装到Iterable对象中 . 但例外仍然是一样的 .

public List<Employee> findAllById(List<String> empIds) {
        Iterable<String> itrKeys = empIds;
        return employeeRepo.findAll(itrKeys);
    }

此外,我尝试使用N1QL和@query over方法

@Query("#{#n1ql.selectEntity} WHERE meta().id IN $ids AND #{#n1ql.filter}")
Collection<ProductCopy> findAllById(@Param("ids") JsonArray ids);

我将我的密钥列表转换为JsonArray . 以上findAllById()方法不会抛出任何异常,但即使我有匹配的密钥也不提供任何文档 .

@Query("#{#n1ql.selectEntity} USE KEYS ($ids) ")
Collection<ProductCopy> findByIdIn(@Param("ids") JsonArray ids);

并且在执行findByIdIn()时,我得到此异常n1ql错误:{“msg”:“主键丢失或无效 .

My question is why findAll(Iterable id)/findAll(List id) is not working,when findOne(String id) works smooth and how do I create a parameterized N1QL query which can take multiple items in an IN statement?

1 回答

  • 2

    尝试添加@ N1qlPrimaryIndexed和@ViewIndexed注释,如下例所示:

    @N1qlPrimaryIndexed
    @ViewIndexed(designDoc = "businessUnity")
    public interface BusinessUnityRepository extends CouchbaseRepository<BusinessUnity, String>{
    
    List<BusinessUnity> findByCompanyId(String companyId);
    
    }
    

    此外,您还可以查看本教程:

    https://blog.couchbase.com/couchbase-spring-boot-spring-data/

相关问题