在google appengine [Java]中根据__key__(唯一标识符)进行选择
我
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 过滤器值必须是键;收到 aght52oobW1hIHTWVzc2FnZRiyAQw (a str)。
我也尝试过使用,
gql = "select * from QuantityType where __key__=='" + KeyFactory.stringToKey(qTypeKey)+"'";
但它不起作用。
如何通过密钥从数据存储中获取特定对象?
I have
public class QuantityType {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
@Persistent
private String type;
}
I am trying to setup a query to get the right QuantityType by it's key
gql = "select * from QuantityType where __key__='aght52oobW1hIHTWVzc2FnZRiyAQw'";
But its not working because
BadFilterError: BadFilterError: invalid filter: key filter value must be a Key; received aght52oobW1hIHTWVzc2FnZRiyAQw (a str).
I have also tried to use
gql = "select * from QuantityType where __key__=='" + KeyFactory.stringToKey(qTypeKey)+"'";
but it's not working..
How can I get a specific object from my datastore by it's key?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您永远不应该手动构建 GQL 字符串 - 这会导致注入漏洞。相反,请声明并传入参数,如此处。
不过,要按键检索实体,您根本不需要执行查询:使用 getObjectById,如文档所述 此处。这比使用查询要快得多。
First, you should never construct a GQL string by hand - this leads to injection vulnerabilities. Instead, declare and pass in parameters, as documented here.
To retrieve an entity by key, though, you don't need to do a query at all: Use getObjectById, as documented here. This is significantly faster than using a query.