【问题标题】:Objectify does not fetch by String IDObjectify 不按字符串 ID 获取
【发布时间】:2014-10-21 13:01:49
【问题描述】:

我有一个 ID 字段为字符串的数据存储类型

ofy().load().type( Scores.class ).id( playerName ).now();

这会获取 null。我已确认具有给定玩家名称的实体存在。 这不会发生在另一个 ID 很长的 Kind 上。

分数类代码

import com.googlecode.objectify.Key;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;
import com.googlecode.objectify.annotation.Parent;

@Entity
public class Scores
{
    @Parent
    public Key<RankerRoot> parentKey;
    @Id
    public String  playerName;
    public int value;
}

【问题讨论】:

  • 你能给出你的Scores Class的代码吗?目前你可以尝试做plarName.trim()。一年前,我在一个应用程序中遇到了字符串格式的问题。
  • 您是否还可以看到键名为 playerName 的实体存在于数据存储中(使用数据存储查看器)并且它没有祖先?
  • @tx802 加油!它有一个祖先。我应该看到它的到来。让我尝试在语句中添加祖先()。
  • @Pintouch 用代码更新问题。
  • 您可以使用Key&lt;Scores&gt; key = Key.create(parentKey, Scores.class, playerName) 单独创建密钥,然后调用ofy().load().key(key).now() - 可以吗?

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


【解决方案1】:

您需要确保为 Objectify 提供了足够的信息来构造实体的 Key。因此,如果实体有 ancestor,则仅 ID/名称是不够的。

如果你的实体有祖先,你可以构造Key,然后通过Key加载实体,像这样:

Key<Scores> scoreKey = Key.create(parentKey, Scores.class, playerName);
ofy().load().key(scoreKey).now();

【讨论】:

    猜你喜欢
    • 2014-01-04
    • 2013-06-18
    • 1970-01-01
    • 1970-01-01
    • 2012-06-29
    • 2016-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多