[英]Spring CrudRepository query with child element?
i have couchbase document like following我有像下面这样的沙发床文件
{
"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 class文档类
@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
}
Service服务
@Service
public interface ContentDocRepository extends CrudRepository<ContentDoc, String> {
public List<ContentDoc> findByReadersIn(String reader) throws Exception;
}
Testcase测试用例
@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)
}
}
I wrote code as per above but not able to retrieve result always got empty arraylist.我按照上面写的代码但无法检索结果总是得到空的arraylist。
Anyone knows what going wrong with my code and how can i execute query with child element?任何人都知道我的代码出了什么问题以及如何使用子元素执行查询?
Thanks in advances.感谢提前。
After long RND and experiment i got solution,经过长时间的RND和实验,我得到了解决方案,
we dont have way to finding child element with method name so we need to do as per my following answer
我们没有办法找到具有方法名称的子元素,所以我们需要按照我的以下答案进行操作
Steps :步骤:
viewname : findContentByUser视图名称:findContentByUser
function (doc, meta) {
if(doc._class == "package.model.ContentDoc") {
for(var i=0; i < doc.contributors.length; i++){
emit(doc.contributors[i].id, null);
}
}
}
Repository : binding viewname and designDocument with impl method as per following存储库:使用 impl 方法绑定 viewname 和 designDocument,如下所示
@Repository public interface ContentDocRepository extends CrudRepository<ContentDoc, String> { @View(viewName = "findContentByUser", designDocument="dev_content") public List<ContentDoc> findByContributors_id(String id); }
Finally Got result :)终于有结果了:)
You don't need to create a view anymore, just use the @N1qlPrimaryIndexed and @ViewIndexed annotations and it should work out-of-the-box:您不再需要创建视图,只需使用 @N1qlPrimaryIndexed 和 @ViewIndexed 注释,它应该开箱即用:
@N1qlPrimaryIndexed
@ViewIndexed(designDoc = "building")
public interface BuildingRepository extends
CouchbasePagingAndSortingRepository<Building, String> {
List<Building> findByCompanyId(String companyId);
}
I answered a very similar question here Using IN clause in couchbase N1Ql @query or use findAll(keys) from couchbase JPA我在这里回答了一个非常相似的问题Using IN 子句 in couchbase N1Ql @query or use findAll(keys) from couchbase JPA
And you can follow my tutorial here: https://blog.couchbase.com/couchbase-spring-boot-spring-data/您可以在此处按照我的教程进行操作: https : //blog.couchbase.com/couchbase-spring-boot-spring-data/
@Service
public interface ContentDocRepository extends CrudRepository<ContentDoc, String> {
@View(viewName = "findContentByUser", designDocument="dev_content")
public List<ContentDoc> findByContributors_id(String id) throws Exception;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.