无法通过Objectify Ref获得Entity

时间:2017-11-21 17:47:11

标签: java google-app-engine google-cloud-datastore objectify

我是客观化和数据存储的新手。我有三种1-用户,2-post,3-feed 用户是帖子的父级,Feed和Feed包含帖子的参考。

饲料

public class Feed {

@Id Long id;
@Parent Key<User> userKey;
Ref<Post> post;

发表

public class Post {

@Id Long id;

@Parent Key<User> userKey;

String content;

获取实体

Key key = Key.create(User.class, 124);
    Query query = ofy().load().type(Feed.class).ancestor(key);

    Feed feed = (Feed) query.list().get(0);

    out.print(feed.getPost()); // return LiveRef(Key<?>(Post(4644337115725824)))
    out.print(feed.getPost().get()); // return null

我想获得Post的内容。我怎么能这样做?

更新

在数据存储区中存在此ID的帖子。那我为什么得到null

更新2

我从数据存储中删除所有内容并再次插入以仔细检查。 但还是一样的。无法获得Post返回null。 我还尝试使用@Load@Index注释仍然无法获得此功能。

 @Load Ref<Post> post;

并试试这个。

 @Index Ref<Post> post;

两者都不起作用。
这是我的数据存储区值。

用户

 key                                        ID/Name         name
 ---------------------------------------------------------------------
 aglub19hcHBfaWRyCgsSBFVzZXIYeww            123             Azeem(unindexed)
 aglub19hcHBfaWRyCgsSBFVzZXIYfAw            124             Arfan(unindexed)

发表

 Key                            ID/Name                   content
 ------------------------------------------------------------------ 
 aglub19hcHBfaWRyGwsSBFVzZXIYewwLEgRQb3N0GICAgICAgLgKDA       5875790138834944   This is First Post (unindexed)     

FEED

key                                                   ID/Name         post
----------------------------------------------------------------------------
aglub19hcHBfaWRyGwsSBFVzZXIYfAwLEgRGZWVkGICAgICAgIQIDA   4521191813414912  Post(5875790138834944) (unindexed)      

当我运行 GetEntity

时,我得到了什么
 out.print(feed.getPost()); // return LiveRef(Key<?>(Post(5875790138834944)))
out.print(feed.getPost().get()); // return null

1 个答案:

答案 0 :(得分:2)

用户是Post的孩子。您无法在不知道父ID的情况下访问子实体。您的ID应如下LiveRef(Key<?>(User(XYZ)/Post(4644337115725824)))