2010-05-23 67 views
1

我有选择基于__key__(唯一标识符)在谷歌的AppEngine【JAVA]

public class QuantityType { 
    @PrimaryKey 
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) 
    private Key key; 

    @Persistent 
    private String type; 
} 

我想设置一个查询,以获得正确的QuantityType通过它的关键

gql = "select * from QuantityType where __key__='aght52oobW1hIHTWVzc2FnZRiyAQw'"; 

但它不工作,因为

BadFilterError:BadFilterError:invalid filter:key filter value must be a Key;得到了一个强大的支持。

我也曾尝试使用

gql = "select * from QuantityType where __key__=='" + KeyFactory.stringToKey(qTypeKey)+"'"; 

,但它不工作..

如何从我的数据存储获得一个特定的对象由它的键?

回答

2

首先,您不应该手工构建GQL字符串 - 这会导致注入漏洞。相反,声明和传入参数,如记录here

但是,通过键检索实体,根本不需要执行查询:使用getObjectById,如文档here所述。这比使用查询要快得多。

+1

并且解决方案是 QuantityType q = pm.getObjectById(QuantityType.class,KeyFactory.stringToKey(qTypeKey)); 谢谢 – Stavros 2010-05-24 02:06:19