Spring CrudRepository query with child element?

自作多情 提交于 2019-12-24 06:47:41

问题


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.

Anyone knows what going wrong with my code and how can i execute query with child element?

Thanks in advances.


回答1:


After long RND and experiment i got solution,

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

Steps :

1) Create custom view in couchbase as per following

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 : 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;

}

Finally Got result :)




回答2:


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(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

And you can follow my tutorial here: https://blog.couchbase.com/couchbase-spring-boot-spring-data/




回答3:


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

    @View(viewName = "findContentByUser", designDocument="dev_content")
    public List<ContentDoc> findByContributors_id(String id) throws Exception;

}


来源:https://stackoverflow.com/questions/38758878/spring-crudrepository-query-with-child-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!