2010-06-10 69 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