简体   繁体   English

在Objectify中合并父子实体

[英]Combine parent child entities in Objectify

I'm in datastore and objectify I have two Kinds 1-posts 2-comments. 我在数据存储区中,对象化了,我有两个Kinds 1-posts 2-comments。
Post is Parent of comment. 帖子是评论的父级。

I want to get all posts with recent 3 comments in objectify. 我想在objectify中获得所有带有最近3条评论的帖子。
I know how can I get the comments using ancestor relation. 我知道如何使用祖先关系获得评论。 But how I combine these with posts. 但是我如何将这些与帖子结合在一起。

ofy().load().type(Comments.Class).ancestor(parentPostKey).limit(3).list();

I already know the id of Post so getting post is also very easy 我已经知道Post的ID,所以发布帖子也很容易

ofy().load().key(key).now(); ofy()。load()。key(key).now();

But how can I combine these two queries in one. 但是,如何将这两个查询合而为一。 And get result back in single class.Something like this. 然后将结果返回到单个类中。

ResultClass results = // Combine query

// Foreach
results.getPost().getContent();
results.getComment().getContent();
// end Foreach

Is it possible or not ? 有可能吗? If Not what's the alternative. 如果不是,那有什么选择。

Your Suggestions Please. 请提出您的建议。

You don't need to make comments children of Posts. 您无需将评论设为Posts的子级。 That will limit you in various ways, most important being that the rate at which you'll be able to add comments to posts will be limited to about 1/second (because they will all be in the same entity group). 这将以各种方式限制您的使用,最重要的是,您可以在帖子中添加评论的速率将被限制为大约1 /秒(因为它们都在同一实体组中)。

Instead, simply save the Key<Post> in your Comment entity: 相反,只需将Key<Post>保存在Comment实体中:

@Entity
public class Comment {

    //...

    @Index
    Key<Post> parentPost;

    //...
}

Then simply query for all the comments that have parentPost set to a particular value: 然后,只需查询所有将parentPost设置为特定值的注释即可:

ofy().load().type(Comment.class).filter("parentPost", postKey.getRaw()).limit(3).list();

Alternatively, you could store the Key s of the Comment entities in the Post class in a List<Key<Comment>> and then use the @Load annotation to fetch them efficiently, but you'll face the same rate-limiting problem as before. 另外,您可以将Post类别中Comment实体的Key存储在List<Key<Comment>> ,然后使用@Load批注有效地获取它们,但是您将面临与之前相同的速率限制问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM