How can I map a parent-child relationship in JDO, where each parent has a list of children?

发布于 2022-09-06 12:37:12 字数 2252 浏览 15 评论 0

I'm new to JDO but I'm willing to use it to to make my code portable (currently I'm using AppEngine, thats why). In the following Google Talk: http://dl.google.com/io/2009/pres/W_0415_Building_Scalable_Complex_App_Engines.pdf, Brett Slatkin showed a new and efficient pattern to retrieve parent entities using child's keys [slides 23 - 25]. In his example, he didn't have any mapping of parent-child relationships for the classes in Python. I don't know how Python works, but I'm guessing in JDO, you need to specify the parent somewhere. So for the following code, how can I map MessageIndex as a child to Message:

// Message.java
@PersistenceCapable
public class Message {
    @PrimaryKey
    @Persistent(valueStrategy=IdGeneratorStrategy.IDENTITY)
    Long id;

    @Persistent String sender;
    @Persistent Text body;
}

// MessageIndex.java
public class MessageIndex {
    @PrimaryKey
    @Persistent(valueStrategy=IdGeneratorStrategy.IDENTITY)
    Long id;

    @Persistent List<String> receivers;
}

// Query.java - Query Example
Query query = pm.newQuery("select id from MessageIndex" +
                          "where receivers == NameParam " +
                          "parameters String NameParam");
List<Key> indexes = (List<Key>) query.execute("Smith"); // List the child keys here
List<Key> keys = new List<Key>; // A place to store parent keys
for (Key k : indexes)
{
    keys.add(k.getParent()); // Here, how would getParent() know who it's parent is?
}

List <Message> messages = new List <Message>
for (Key k : keys)
{
    messages.add(pm.getObjectById(Message.class, k));
}

So, how can I map the parent-child relationship between Message and MessageIndex in this example? Did I convert the Python code correctly?

Any comments/suggestions are highly welcome!

P.S. The link for the video is here: http://www.google.com/events/io/2009/sessions/BuildingScalableComplexApps.html --> very interesting!

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花落人断肠 2022-09-13 12:37:12

Never mind. I found another way of doing this. Whenever I create a Message, I'll create the MessageIndex with the same key. This way, MessageIndex is always associated with a Message because they share the same key. Now, I can simply getKey() from MessageIndex and use getObjectById() in the Message entity to get the corresponding message. I would appreciate it if anyone had any better solutions than this, but for now I'll go with this method. Thanks anyways.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文