我们如何构建Spring JPA查询并检索嵌套的Couchbase文档值的数据? 我试图避免编写本机查询,而是使用JPA方法基于嵌套2个级别的键来检索数据。
Couchbase文档值是类型
Company
员工是一个列表,而任务是员工下的一个属性,员工是一个以详细信息为对象的列表
我想查询多个文档并按categoryId检索匹配的记录。
将存储库接口扩展到传递Company和文档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
}
]
答案 0 :(得分:1)
您不能使用Spring Data查询嵌套实体,因为它不是原始Spring Data规范的一部分。但是,您只需使用 @Query 批注
编写N1QL查询例如:
@Query("#{#n1ql.selectEntity} where #{#n1ql.filter} and companyId = $1 and " +
" removed = $2 and lower(name) like $3 order by lower(name) asc LIMIT $4 OFFSET $5 ")
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