本文介绍了Spring CrudRepository查询与子元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有如下所述的bedfa文档
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
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
public interface ContentDocRepository extends CrudRepository<ContentDoc, String> {
public List<ContentDoc> findByReadersIn(String reader) throws Exception;
}
测试用例
@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.
任何人都知道我的代码出了什么问题以及如何使用子元素执行查询?
Anyone knows what going wrong with my code and how can i execute query with child element?
预先感谢。
推荐答案
经过长时间的RND和实验后,我得到了解决方案,
After long RND and experiment i got solution,
步骤:
1)按照以下步骤在沙发床中创建自定义视图
1) Create custom view in couchbase as per following
viewname :findContentByUser
viewname : 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);
}
}
}
2) Service :按照以下方式使用impl方法绑定视图名称和designDocument
2) Service : binding viewname and designDocument with impl method as per following
@Service
public interface ContentDocRepository extends CrudRepository<ContentDoc, String> {
@View(viewName = "findContentByUser", designDocument="dev_content")
public List<ContentDoc> findByContributors_id(String id) throws Exception;
}
最终得到结果:)
这篇关于Spring CrudRepository查询与子元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!