gpt4 book ai didi

java - Spring CrudRepository 查询子元素?

转载 作者:行者123 更新时间:2023-11-30 02:50:39 25 4
gpt4 key购买 nike

我有如下的 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

}

服务

@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)
}
}

我按照上面编写的代码,但无法检索结果总是得到空数组列表。

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

提前致谢。

最佳答案

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

we dont have way to finding child element with method name so we needto do as per my following answer

步骤:

  1. 按照以下步骤在 couchbase 中创建自定义 View

View 名称: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:按照以下方式使用 impl 方法绑定(bind) viewname 和 designDocument

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

    @View(viewName = "findContentByUser", designDocument="dev_content")
    public List<ContentDoc> findByContributors_id(String id);
    }
  • 终于得到结果了:)

    关于java - Spring CrudRepository 查询子元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38758878/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com