如何在 JDO 中映射父子关系,其中每个父级都有一个子级列表?
我是 JDO 的新手,但我愿意用它来使我的代码可移植(目前我正在使用 AppEngine,这就是原因)。在以下 Google Talk 中:http://dl.google.com/io/2009 /pres/W_0415_Building_Scalable_Complex_App_Engines.pdf,Brett Slatkin 展示了一种使用子密钥检索父实体的新且高效的模式 [幻灯片 23 - 25]。在他的示例中,他没有对 Python 中的类进行任何父子关系映射。我不知道Python是如何工作的,但我猜在JDO中,你需要在某处指定父级。那么对于下面的代码,如何将MessageIndex作为子项映射到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));
}
那么,如何映射本例中的Message和MessageIndex之间的父子关系呢?我是否正确转换了 Python 代码?
非常欢迎任何意见/建议!
PS 视频的链接位于:http://www.google。 com/events/io/2009/sessions/BuildingScalableComplexApps.html -->很有趣!
谢谢。
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)
没关系。我找到了另一种方法来做到这一点。每当我创建消息时,我都会使用相同的键创建 MessageIndex。这样,MessageIndex 始终与 Message 关联,因为它们共享相同的密钥。现在,我可以简单地从 MessageIndex 中 getKey() 并在 Message 实体中使用 getObjectById() 来获取相应的消息。如果有人有比这更好的解决方案,我将不胜感激,但现在我将采用这种方法。无论如何,谢谢。
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.