如何将 Spring JPA 用于 Couchbase 嵌套文档值

How to use Spring JPA for Couchbase nested document values

我们如何为嵌套的 Couchbase 文档值构建 Spring JPA 查询和检索数据? 我试图避免编写本机查询并使用 JPA 方法根据嵌套 2 级的键检索数据。

couchbase 文档值是类型 Company 其中 employees 是一个列表,而 tasks 是一个 属性 下的 employee ,它是一个包含详细信息作为对象的列表

我想查询多个文档并通过categoryId检索匹配的记录。

将存储库接口扩展到传递公司和文档 ID 的 CouchbaseRepository。 我试过

// finding by Employees-->Tasks-->Details-->CategoryId 
findByEmployeesTasksDetailsCategoryId(Integer id); // Does not work

但不起作用

"company": "Xyc",
"employees": {
   "name": "John Smith",
   "age": 24,
   "tasks": [
       {
            "id": 231,
            "date": "05-13-2019"
            "details": {
                    "categoryName": "Software",
                    "categoryId": 12,     
                    "description": "Buy Software"
                    "location": "Plano, Texas"
                    "zip": 75024
                }
            }
        },
       {
            "id": 789,
            "date": "05-14-2019"
            "details": {
                    "categoryName": "Hardware",
                    "categoryId": 17,     
                    "description": "Buy hardware"
                    "location": "Irving, Texas"
                    "zip": 75038
                }
            }
        },
        {
              "id": 456,
              "date": "05-15-2019"
               "details": {
                    "categoryName": "Hardware",
                    "categoryId": 17,     
                    "description": "Buy hardware"
                    "location": "Plano, Texas"
                    "zip": 75024
                 }
        }
    ]
}

我正在寻找 JPA 方法,在那里我可以通过 categoryId 获取详细信息任务或详细信息。

categoryId 17 的预期输出

[
   {
      "categoryName": "Hardware",
      "categoryId": 17,     
      "description": "Buy keyboard and mouse"
      "location": "Irving, Texas"
      "zip": 75038
   },
   {
      "categoryName": "Hardware",
      "categoryId": 17,     
      "description": "Buy monitor"
      "location": "Plano, Texas"
      "zip": 75024
   }

]

您不能使用 Spring 数据来查询嵌套实体,因为它不是原始 Spring 数据规范的一部分。但是,您可以使用 @Query 注释

简单地编写 N1QL 查询

例如:

    @Query("#{#n1ql.selectEntity} where #{#n1ql.filter} and companyId =  and " +
        " removed =  and lower(name) like  order by lower(name) asc LIMIT  OFFSET   ")
    List<FamilyResource> listFamilies(String companyId, boolean removed, String name, int limit, int offset);

对于你的情况,你可以简单地使用 UNNEST https://docs.couchbase.com/server/6.0/n1ql/n1ql-language-reference/unnest.html