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"); // 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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.