如何在无主关系模型中查询JDO持久对象?
我正在尝试将我的应用程序从 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 = '[email protected]'
据我了解,这种方式不支持 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 个查询,但这似乎是错误的,并且会产生不必要的开销。请帮我提供一些代码示例。
-- 保罗.
I'm trying to migrate my app from PHP and RDBMS (MySQL) to Google App Engine and have a hard time figuring out data model and relationships in JDO. In my current app I use a lot of JOIN queries like:
SELECT users.name, comments.comment
FROM users, comments
WHERE users.user_id = comments.user_id
AND users.email = '[email protected]'
As I understand, JOIN queries are not supported in this way so the only(?) way to store data is using unowned relationships and "foreign" keys. There is a documentation regarding that, but no useful examples. So far I have something like this:
@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...
}
So, how do I get a list with commenter's name, comment and date in one query? I see how I probably could get away with 3 queries but that seems wrong and would create unnecessary overhead. Please, help me out with some code examples.
--
Paul.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
的有效 JDOQL
只能评论SELECT this.name, commentVar.comment FROM mydomain.Users
WHERE this.key == commentVar.userKey && this.email = '[电子邮件受保护]'
VARIABLES mydomain.Comments commentVar
GAE/J 是否认为实现它由 Google 来回答。
Can only comment on what is valid JDOQL for that
SELECT this.name, commentVar.comment FROM mydomain.Users
WHERE this.key == commentVar.userKey && this.email = '[email protected]'
VARIABLES mydomain.Comments commentVar
whether GAE/J deems to implement it is for Google to answer.