Google 端点查询 returns 仅 "etag" 和 "kind" 而不是集合

Google Endpoints query returns only "etag" and "kind" instead of collection

在我的 Google 端点实现中,我创建了一个简单的 POJO,如下所示:

@Entity
public class Cartoon {
    @Id
    private Long id;

    private Long channelId;

    @Parent
    @ApiResourceProperty(ignored = AnnotationBoolean.TRUE)
    private Key<Channel> channelKey;

    @Index
    @SerializedName("name")
    private String name;

    private Cartoon() {}

    public Cartoon(final long id, final long channelId,
            final CartoonForm cartoonForm) {
        this.id = id;
        this.channelKey = Key.create(Channel.class, channelId);
        this.channelId = channelId;
        updataFromCartoonForm(CartoonForm);
    }

    public long getId() { return id; }

    @ApiResourceProperty(ignored = AnnotationBoolean.TRUE)
    public Key<Channel> getChannelKey() { return channelKey; }

    public long getChannelId() { return channelId; }

    public String getWebSafeKey() {
        return Key.create(channelKey, Cartoon.class, id).getString();
    }

    public String getName() { return name; }

    public void updateWithCartoonForm(CartoonForm CartoonForm) {
        name = CartoonForm.getName();
    }

    public String toString() { return new Gson().toJson(this); }

正确创建了两个 Cartoon 类型的对象,但是查询

Key<Channel> channelKey = Key.create(channelId);
return ofy().load().type(Cartoon.class).ancestor(channelKey).list();

returns结果200及以下JSON

{
    "kind": "channel#resourcesItem",
    "etag": "\"HvVI34INX8_n_JiuB-aYaj2l4Mg/ulgdYniw-Q\""
}

每次创建新的 Cartoon 对象时,都会将此对象的网络安全密钥添加到祖先 Channel 对象内的 String 列表中。所以奇怪的是,如果从 List<String> 创建一个 List<Key<Cartoon>> 并调用

return ofy().load().keys(cartoonKeys).values();

方法returns预期结果是什么:

{
 "items": [
  {
   "id": "180001",
   "name": "Adventure Time",
   "webSafeKey": "abcdefghij",
   "kind": "channel#resourcesItem"
  },
  {
   "id": "190001",
   "name": "The Amazing World of Gumball",
   "webSafeKey": "aghzfm5vcW",

   "kind": "channel#resourcesItem"
  }
 ],
 "kind": "channel#resources",
 "etag": "\"HvVI34INX8_n_JiuB-aYaj2l4Mg/ulgdYniw-Q\""
}

什么会导致此行为?

问题是 Channel POJO 也有一个名为 Satellite 的祖先,因此不可能从频道 ID 重新创建 Key<Channel> 对象。因此查询工作 完美 ,即它没有找到 Cartoon 对象,因为通知的祖先键不存在。

查看 com.googlecode.objectify.Key implementation on Google Code repository,您注意到,如果祖先也有一个祖先,您应该像这样重新创建密钥:

Key<Satellite> satelliteKey = Key.create(Satellite.class, satelliteId);
Key<Channel> channelKey = Key.create(satelliteKey, Channel.class, channelId);

但是你有一个有效的 webSafeChannelKey 字符串对象,你可以调用:

Key<Channel> channelKey = Key.create(webSafeChannelKey);