Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 带子元素的Spring crudepository查询?_Java_Spring Boot_Couchbase - Fatal编程技术网

Java 带子元素的Spring crudepository查询?

Java 带子元素的Spring crudepository查询?,java,spring-boot,couchbase,Java,Spring Boot,Couchbase,我有如下couchbase文档 { "contentTimestamp": 1470216079085, "version": 12, "content": [ { "text": "ABC", "params": { "TYPE": "TEXT" } } ], "readers": { "u_id_1": 0, "u_id_2": 0, }, "contributors": [

我有如下couchbase文档

{
  "contentTimestamp": 1470216079085,
  "version": 12,
  "content": [
    {
      "text": "ABC",
      "params": {
        "TYPE": "TEXT"
      }
    }
    ],
  "readers": {
    "u_id_1": 0,
    "u_id_2": 0,
  },
  "contributors": [
    {
      "id": "u_id_1"
    }
  ]
}
文档类

@Document
public class ContentDoc implements Serializable{

    private static final long serialVersionUID = 1L;


    @Id
    private String id;

    @Field
    private Integer version = 12;

    @Field
    private List<Content> content = new ArrayList<>();

    @Field
    private Map<String, Object> readers = new HashMap<>();

    //etc

    //getter setter

}
@文档
公共类ContentDoc实现了可序列化{
私有静态最终长serialVersionUID=1L;
@身份证
私有字符串id;
@场
私有整数版本=12;
@场
私有列表内容=新的ArrayList();
@场
私有映射读取器=新HashMap();
//等
//吸气剂设定器
}
服务

@Service
public interface ContentDocRepository extends CrudRepository<ContentDoc, String> {

    public List<ContentDoc> findByReadersIn(String reader) throws Exception;

}
@服务
公共接口ContentDocRepository扩展了Crudepository{
公共列表findByReadersIn(字符串读取器)引发异常;
}
测试用例

@RunWith(SpringJUnit4ClassRunner.class)
public class Tests {

    @Autowired
    private ContentDocRepository contentDocRepository;

    @Test
    public void cotentDocRepoTest(){

        List<ContentDoc> contents = contentDocRepository.findByReadersIn("u_id_1");
        Assert.assertNotNull(contents);
        System.out.println(contents)
    }
}
@RunWith(SpringJUnit4ClassRunner.class)
公开课考试{
@自动连线
私有ContentDocRepository ContentDocRepository;
@试验
公共无效cotentDocRepoTest(){
List contents=contentDocRepository.findByReadersIn(“u_id_1”);
Assert.assertNotNull(内容);
系统输出打印项次(目录)
}
}
我按照上面的代码编写了代码,但无法检索结果,结果总是得到空的arraylist

有人知道我的代码出了什么问题,以及我如何使用子元素执行查询吗


提前谢谢你

经过长时间的研究和实验,我得到了解决方案

我们无法找到方法名为的子元素,因此需要 按照我下面的回答去做

步骤:

  • 在couchbase中创建自定义视图,如下所示
  • 视图名称:FindContentByser

    function (doc, meta) {
      
      if(doc._class == "package.model.ContentDoc") {
        for(var i=0; i < doc.contributors.length; i++){
             emit(doc.contributors[i].id, null);
        }
           
      }
    }  
    
    函数(文档、元){
    if(doc.\u class==“package.model.ContentDoc”){
    对于(变量i=0;i
  • 存储库:使用impl方法绑定viewname和designDocument,如下所示

     @Repository
     public interface ContentDocRepository extends CrudRepository<ContentDoc, String> {
    
        @View(viewName = "findContentByUser", designDocument="dev_content")
        public List<ContentDoc> findByContributors_id(String id);   
    }
    
    @存储库
    公共接口ContentDocRepository扩展了Crudepository{
    @视图(viewName=“FindContentByser”,designDocument=“开发内容”)
    公共列表findByContributors_id(字符串id);
    }
    
  • 最终得到结果:)

    @Service
    公共接口ContentDocRepository扩展了Crudepository{
    @视图(viewName=“FindContentByser”,designDocument=“开发内容”)
    公共列表findByContributors_id(字符串id)引发异常;
    }
    
    您不再需要创建视图,只需使用@N1QLPRIMARYINDEX和@VIEWINDEX注释即可,它应该是现成的:

    @N1qlPrimaryIndexed
    @ViewIndexed(designDoc = "building")
    public interface BuildingRepository extends 
    CouchbasePagingAndSortingRepository<Building, String> {
    
        List<Building> findByCompanyId(String companyId);
    
    }
    
    @N1qlPrimaryIndexed
    @视图索引(designDoc=“building”)
    公共接口BuildingRepository扩展
    CouchBasePaging和SortingRepository{
    列出findByCompanyId(字符串companyId);
    }
    
    我在这里回答了一个非常类似的问题

    您可以按照我的教程进行操作:

    谢谢,这非常有用。请解释代码并更好地放置标记“代码”,上面已经解释过了。我不知道你为什么要重复。这个问题与在顶部子元素过滤器上提取父元素有关。
    @N1qlPrimaryIndexed
    @ViewIndexed(designDoc = "building")
    public interface BuildingRepository extends 
    CouchbasePagingAndSortingRepository<Building, String> {
    
        List<Building> findByCompanyId(String companyId);
    
    }