user2054833
user2054833

Reputation: 2165

Spring Data Neo4j Pageable

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?

Upvotes: 3

Views: 2546

Answers (3)

kalaivani sivasami
kalaivani sivasami

Reputation: 1

Try this works for me

Add page parameter as below

parameters.put("page", pageable.getPageNumber() * pageable.getPageSize());
parameters.put("size", pageable.getPageSize());

and append SKIP $page LIMIT $size in your query.

Upvotes: 0

Wojtek Wysocki
Wojtek Wysocki

Reputation: 488

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

Upvotes: 1

PhilBa
PhilBa

Reputation: 732

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.

Upvotes: 4

Related Questions