【问题标题】:How to query JDO persistent objects in unowned relationship model?如何在无主关系模型中查询 JDO 持久对象?
【发布时间】: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&lt;Comments&gt; 而不是Set&lt;Key&gt;。我建议在类名中也使用单数。

标签: google-app-engine persistence entity-relationship jdo


【解决方案1】:

只能评论什么是有效的 JDOQL

从 mydomain.Users 中选择 this.name、commentVar.comment WHERE this.key == commentVar.userKey && this.email = 'john@example.com' 变量 mydomain.Comments commentVar

GAE/J 是否认为实施是由 Google 来回答的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-15
    • 2016-11-16
    • 2015-03-15
    • 1970-01-01
    • 1970-01-01
    • 2013-07-21
    • 1970-01-01
    • 2023-03-20
    相关资源
    最近更新 更多