Pagination in Google App Engine with Java

后端 未结 4 430
攒了一身酷
攒了一身酷 2021-01-12 11:23

I need to create simple pagination of objects, but when I read manual I found out that query.setRange(5, 10); will fetch 10 objects, even when only 5 objects are needed.

4条回答
  •  囚心锁ツ
    2021-01-12 11:34

    How about this:

    List results = (List) query.execute();
    // Use the first 20 results...
    
    Cursor cursor = JPACursorHelper.getCursor(results);
    String cursorString = cursor.toWebSafeString();
    // Store the cursorString...
    
    // ...
    
    // Query query = the same query that produced the cursor
    // String cursorString = the string from storage
    Cursor cursor = Cursor.fromWebSafeString(cursorString);
    query.setHint(JPACursorHelper.CURSOR_HINT, cursor);
    query.setRange(0, 20);
    
    List results = (List) query.execute();
    // Use the next 20 results...
    

    From:

    How to use datastore cursors with jpa on GAE

    Also:

    http://groups.google.com/group/google-appengine-java/browse_thread/thread/5223215ff24c3b3e/d22297d1d76a9c8b

    Or without JPA see:

    http://code.google.com/appengine/docs/java/javadoc/com/google/appengine/api/datastore/Cursor.html

提交回复
热议问题