Spring Data Neo4j Pageable

自古美人都是妖i 提交于 2020-01-02 07:15:19

问题


I have a Spring Data Repository interface that gets a collection of nodes from by database using a custom query:

Repository Method:

@Query ("START r = node({0}) MATCH r <-[:AUTHOR]-  m RETURN m")
public Page<Object> findObjectById (long objectId,Pageable pageable);

Method call

 custRepository.findObjectById (4,new PageRequest(0, 5));

This return a collection of Objects, however the Page information is incorrect. There is enough data in database for me to get several pages of data. The first Page information is saying that there is:

 Current Page #: 0
 Total Pages: 1
 Is First Page: true
 Is last Page: true

However when I fetch the second page I am still getting a collection of the other objects and the page information then becomes:

 Current Page #: 1
 Total Pages: 6
 Is First Page: false
 Is last Page: false

This clearly show that the page information on the first page is incorrect and since I need accurate information to implement pagination in my app this becomes a problem. What is causing this problem and how do I fix it?


回答1:


I hand the same problem. Turns out you have to also specify the countQuery attribute of the query annotation. Not sure if its a bug or not so well documented. In your case it should be

@Query (value="START r = node({0}) MATCH r <-[:AUTHOR]-  m RETURN m", 
        countQuery="START r = node({0}) MATCH r <-[:AUTHOR]-m RETURN count(m)")

Hope it helps you.




回答2:


I have encoutered the same problem and after some research I found a bug in class org.springframework.data.neo4j.repository.query.GraphRepositoryQuery

following method is the problem

@SuppressWarnings({"unchecked", "rawtypes"})
protected Object createPage(Iterable<?> result, Pageable pageable, Long count) {
    final List resultList = IteratorUtil.addToCollection(result, new ArrayList());
    if (pageable==null) {
        return new PageImpl(resultList);
    }
    long currentTotal;
    if (count!=null) {
        currentTotal = count;
    } else {
        int pageSize = pageable.getPageSize();
        long requestedCountStart = pageable.getOffset() * pageSize;
        long resultSize = resultList.size();
        currentTotal = resultSize == pageSize ? requestedCountStart + pageSize : requestedCountStart+resultSize;
    }
    return new PageImpl(resultList, pageable, currentTotal);
}

and specifically those lines:

        int pageSize = pageable.getPageSize();
        long requestedCountStart = pageable.getOffset() * pageSize;

which translates to requestedCountStart = page * size * size

Does anyone know where is the best place to file a bug report for Neo4j?

BTW I am using spring-data-neo4j version 3.0.0.RELEASE



来源:https://stackoverflow.com/questions/20564661/spring-data-neo4j-pageable

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