2010-09-21 24 views
2

我有了這個代碼(Java中,GAE):在Google App Engine(GAE)上,如何在密鑰/ ID字段上搜索?

// Much earlier: 
playerKey = KeyFactory.keyToString(somePlayer.key); 

// Then, later... 
PersistenceManager pm = assassin.PMF.get().getPersistenceManager(); 

Key targetKey = KeyFactory.stringToKey(playerKey); 
Query query = pm.newQuery(Player.class); 
query.setFilter("__key__ == keyParam"); 
query.declareParameters("com.google.appengine.api.datastore.Key keyParam"); 
List<Player> players = (List<Player>) query.execute(targetKey); // <-- line 200 

生成該錯誤:

javax.jdo.JDOFatalUserException: Unexpected expression type while parsing query. Are you certain that a field named __key__ exists on your object? 
    at org.datanucleus.jdo.NucleusJDOHelper.getJDOExceptionForNucleusException(NucleusJDOHelper.java:354) 
    at org.datanucleus.jdo.JDOQuery.execute(JDOQuery.java:252) 
    at myapp.Player.validPlayerWithKey(Player.java:200) 
    // [etc., snip] 

但我不知道就是了。我想搜索的JDO id字段,我心想,我讀了特殊的名字__key__in the documentation

我既

query.setFilter("__key__ == keyParam"); 

query.setFilter("ID == keyParam"); 

具有相同的結果試了一下。那麼,我做錯了什麼?或者更重要的是,我如何正確地做到這一點?

謝謝!

編輯:爲了完整起見,這裏是(我已經接受正確的基礎上登的答案,)最後,工作代碼:

Player result = null; 
if (playerKey == null) 
{ 
    log.log(Level.WARNING, "Tried to find player with null key."); 
} 
else 
{ 
    PersistenceManager pm = assassin.PMF.get().getPersistenceManager(); 

    try { 
     result = (Player) pm.getObjectById(Player.class, playerKey); 
    } catch (javax.jdo.JDOObjectNotFoundException notFound) { 
     // Player not found; we will return null. 
     result = null; 
    } 

    pm.close(); 
} 

return result; 

回答

3

如果您的目標是通過鍵獲取對象,那麼您應該使用PersistenceManager的getObjectByID()方法。更多詳情here

另一方面,試圖構建一個查詢來獲取關鍵的東西是你不需要做的事情。雖然這是你將如何使用SQL數據庫時,谷歌數據存儲做不同的事情,這就是那些情況下,而不是通過構造查詢的麻煩,谷歌應用程序引擎可以讓你得到你直接想要的東西之一。畢竟,數據庫中只有一個實體具有特定的關鍵字,所以在這種情況下,您需要的GQL查詢的其餘機制中沒有任何內容,因此可以跳過所有這些實體以提高效率。

+0

工程就像一個魅力。我偷了你的答案(但喊着出)在這個類似SO問題:http://stackoverflow.com/questions/3438586/google-app-engine-filter-by-id,希望這是確定。謝謝! – Olie 2010-09-21 21:58:56

-1

我會建議你使用JPA(http://code.google.com/appengine/docs/java/datastore/usingjpa.html )來訪問你的數據在GAE,它有可以使用廣爲人知並記錄JPA標準(及其JPAQL查詢語言)做這樣的事情,在移植的方式(如果你堅持到JPA標準非常重要的優勢,你的代碼會爲GAE工作,對Hibernate或EclipseLink的未經修改)

+0

JDO標準也是一個廣爲人知的標準,您可以在其他JDO實現中使用相同的JDO代碼(如果您省略了Google的具體細節),如2009年4月谷歌篝火的原始視頻中清楚展示的那樣。 – DataNucleus 2010-09-22 18:31:20

+0

是,但JPA從採納pov一直是IMO的成功。 – Luxspes 2010-09-23 02:46:36

+1

問題是用戶提出了一個問題,他正在使用JDO。他沒有問你會推薦什麼API,只是我該如何做這件事(用JDO,他現在擁有什麼)。 「啊,你可以用JPA做到這一點,但你必須改變你所有的持久性代碼」 ......沒有真正回答他一點辦法;-) – DataNucleus 2010-09-23 12:38:34

相關問題