【发布时间】:2011-03-03 12:29:42
【问题描述】:
我正在尝试将我的应用程序从 PHP 和 RDBMS (MySQL) 迁移到 Google App Engine,并且很难弄清楚 JDO 中的数据模型和关系。在我当前的应用程序中,我使用了很多 JOIN 查询,例如:
SELECT users.name, comments.comment
FROM users, comments
WHERE users.user_id = comments.user_id
AND users.email = 'john@example.com'
据我了解,这种方式不支持 JOIN 查询,因此存储数据的唯一(?)方法是使用无主关系和“外来”键。有关于此的文档,但没有有用的示例。到目前为止,我有这样的事情:
@PersistenceCapable
public class Users {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
@Persistent
private String name;
@Persistent
private String email;
@Persistent
private Set<Key> commentKeys;
// Accessors...
}
@PersistenceCapable
public class Comments {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
@Persistent
private String comment;
@Persistent
private Date commentDate;
@Persistent
private Key userKey;
// Accessors...
}
那么,如何在一个查询中获得包含评论者姓名、评论和日期的列表?我知道我可能如何摆脱 3 个查询,但这似乎是错误的,并且会产生不必要的开销。请帮我提供一些代码示例。
-- 保罗。
【问题讨论】:
-
同时执行连接也会产生开销。我宁愿在
Users对象上放置Set<Comments>而不是Set<Key>。我建议在类名中也使用单数。
标签: google-app-engine persistence entity-relationship jdo