如何使用数据存储区中的属性检索实体

时间:2014-10-13 10:48:13

标签: google-cloud-datastore persistentmanager

是否可以使用属性从gae数据存储中检索实体而不使用密钥?

我可以看到我可以使用以下语法检索带密钥的实体。

quote = mgr.getObjectById(Students.class, id);

是否有替代方案可以让我们使用属性而不是密钥? 或者请建议任何其他方式来达到要求。

谢谢, Karthick。

1 个答案:

答案 0 :(得分:0)

当然这是可能的。把实体的关键想象成一个SQL行的主键(但请不要过分夸大其中 - 关键是它是主键 - 这两个数据存储系统的实现非常不同,当他们不记住这一点时会给人们带来麻烦。

您应该查看here (JDO)以阅读有关JDO查询的内容,或here (JPA)查看有关JPA查询的内容,具体取决于您的帖子引用的mgr类型。对于JDO,您可以这样做:

// begin building a new query on the Cat-kind entities (given a properly annotated
// entity model class "Cat" somewhere in your code)
Query q = pm.newQuery(Cat.class);

// set filter on species property to == param
q.setFilter("species == speciesParam"); 

// set ordering for query results by age property descending
q.setOrdering("age desc"); 

// declare the parameters for this query (format is "<Type> <name>") 
// as referenced above in filter statement
q.declareParameters("String speciesParam");

// run the query 
List<Cat> results = (List<Cat>) q.execute ("siamese");

对于JPA,您可以使用JPQL字符串来运行查询。