Objectify不会通过String ID获取

时间:2014-10-21 13:01:49

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

我有一个数据存储类型,其中ID字段是String

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

这将获取null。我已确认具有给定playername的实体存在。 这种情况不会发生在ID很长的另一种情况下。

分数类代码

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;
}

1 个答案:

答案 0 :(得分:1)

你需要确保给Objectify足够的信息来构建实体的密钥。因此,如果实体具有ancestor,则仅ID /名称不足。

如果您的实体具有祖先,您可以构造密钥,然后按键加载实体,如下所示:

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