2010-06-10 70 views
7

我需要创建简单的对象分页,但是当我阅读手册时,我发现query.setRange(5,10);将获取10个对象,即使只需要5个对象也是如此。使用Java在Google App Engine中分页

有没有办法获取刚才需要的对象?

编辑:我开始赏金,所以你可以告诉我在Java中的简单示例代码工作,然后我会接受你的答案。

回答

0

如果您正在尝试分页,您可能需要考虑使用cursors而不是setRange。

1

为什么从数据库中返回10个对象时会出现问题?你仍然会返回你关心的5个对象(前5个被丢弃)。

我不是问,因为我认为setRange方法是一种非常好的扩展解决方案,但它是一个简单而合理的解决方案,在很多情况下都足够了。

您是否正在计划分页非常大的表格或合并昂贵的连接?如果没有,我会试着用setRange作为分页的起点。

2

如何:

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

另外:

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

或者没有JPA看到:

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