开发者

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

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"); /开发者_StackOverflow/ 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜