在Google App Engine(GAE)上,如何搜索密钥/ ID字段? [英] On Google App Engine (GAE), how do I search on the Key/ID field?

查看:126
本文介绍了在Google App Engine(GAE)上,如何搜索密钥/ ID字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  //很早:
playerKey = KeyFactory.keyToString(somePlayer.key);

//然后,稍后...
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);
列表< Player> players =(List< Player>)query.execute(targetKey); //< - line 200

会产生这个错误:

  javax.jdo.JDOFatalUserException:解析查询时意外的表达式类型。你确定你的对象上有一个名为__key__的字段吗? 
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 __ 在文档中。



我试过了

  query.setFilter(__ key__ == keyParam); 

  query.setFilter(ID == keyParam); 

具有相同的结果。那么,我做错了什么?或者,更重要的是,我该如何正确使用它?



谢谢!

编辑:为了完整起见,下面是最终的工作代码(根据Gordon的答案,我接受的答案是正确的):

 播放器结果=空; 
if(playerKey == null)
{
log.log(Level.WARNING,试图找到具有空键的播放器。);
}
else
{
PersistenceManager pm = assassin.PMF.get()。getPersistenceManager();

try {
result =(Player)pm.getObjectById(Player.class,playerKey);
} catch(javax.jdo.JDOObjectNotFoundException notFound){
//播放器未找到;我们将返回null。
result = null;
}

pm.close();
}

返回结果;


解决方案

如果您的目标是通过键获取对象,那么你应该使用PersistenceManager的getObjectByID()方法。更多详情此处



另一方面,试图构建一个查询来获取关键字的查询是你不需要做的事情。虽然这是您如何使用SQL数据库的工作方式,但Google Data Store会以不同的方式进行处理,这种情况下,Google App Engine可以让您直接获得所需的内容,而不是经历构建查询的麻烦。毕竟,数据库中只有一个实体具有特定的关键字,因此在这种情况下,您需要的GQL查询的其余机制中没有任何内容,因此它可以全部被跳过以提高效率。


I've got this code (Java, GAE):

// 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

which generates this error:

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]

But I'm not sure what it wants. I'm trying to search on the JDO id field, which I I thought I read had the special name __key__, in the documentation.

I've tried it with both

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

and

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

with the same results. So, what am I doing wrong? Or, more importantly, how do I do it correctly?

Thanks!

Edit: For completeness's sake, here is the final, working code (based on Gordon's answer, which I have accepted as correct):

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;

解决方案

If your objective is to get an object by key, then you should use the PersistenceManager's getObjectByID() method. More details here.

As an aside, trying to construct a query to get something by it's key is something you shouldn't need to do. Although this is how you would work with an SQL database, the Google Data Store does things differently, and this is one of those cases where rather than go through the trouble of constructing a query, Google App Engine lets you get what you want directly. After all, you should only have one entity in the database with a particular key, so there's nothing in the rest of the machinery of a GQL query that you need in this case, hence it can all be skipped for efficiency.

这篇关于在Google App Engine(GAE)上,如何搜索密钥/ ID字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆