使用Java在Google App Engine中分页
问题描述:
我需要创建简单的对象分页,但是当我阅读手册时,我发现query.setRange(5,10);将获取10个对象,即使只需要5个对象也是如此。使用Java在Google App Engine中分页
有没有办法获取刚才需要的对象?
编辑:我开始赏金,所以你可以告诉我在Java中的简单示例代码工作,然后我会接受你的答案。
答
有关于App Engine的网站这个题目的文章:
http://code.google.com/appengine/articles/paging.html
的例子是Python,但它们也非常简单,你大概可以翻译成Java相当容易。
Here's a Java implementation,我还没有阅读或测试。
答
为什么从数据库中返回10个对象时会出现问题?你仍然会返回你关心的5个对象(前5个被丢弃)。
我不是问,因为我认为setRange方法是一种非常好的扩展解决方案,但它是一个简单而合理的解决方案,在很多情况下都足够了。
您是否正在计划分页非常大的表格或合并昂贵的连接?如果没有,我会试着用setRange作为分页的起点。
答
如何:
List<Employee> results = (List<Employee>) 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<Employee> results = (List<Employee>) query.execute();
// Use the next 20 results...
来源:
How to use datastore cursors with jpa on GAE
另外:
或者没有JPA看到:
http://code.google.com/appengine/docs/java/javadoc/com/google/appengine/api/datastore/Cursor.html