【问题标题】:How to uniquely identify and fetch children objects in JDO如何在 JDO 中唯一标识和获取子对象
【发布时间】:2010-07-17 08:32:37
【问题描述】:

我只是在学习 JDO 和 GAE,并且非常坚持。

我已经离开了

public class Article {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;
    ...
}

现在也有了父母:

public class ArticleCollection {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;
    private long count
    private Set<Article> articles;
}

但是,执行此操作后,以下按 id 获取文章的代码不再有效。如何唯一标识一个对象?

Article article = (Article)pm.getObjectById(KeyFactory.createKey(Article.class.getName(), id));

非常感谢任何帮助!

【问题讨论】:

    标签: java google-app-engine jdo


    【解决方案1】:

    子项的密钥包含有关其父项的信息。您需要使用包含父 ID 的 KeyFactory 方法。

    createKey(Key parent, java.lang.String kind, long id)
              Creates a new Key with the provided parent from its kind and ID.
    

    查看javadoc 了解更多详情。为了方便起见,还有一个 Builder 类可以让您执行以下操作:

    Key key = new Builder("ArticleCollection", 123).addChild("Article", 1424).getKey();
    

    随着层次结构的深入,这种形式变得更加有用,因为您可以在调用 getKey 之前将一堆 addChilds 链接在一起。

    如果您不知道文章的父级,我认为您会被困在执行 GQL 查询而不是按键获取。

    【讨论】:

      猜你喜欢
      • 2013-04-13
      • 2019-05-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-19
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多