Google app engine Objectify不按字符串ID获取
我有一种数据存储类型,其中ID字段是StringGoogle app engine Objectify不按字符串ID获取,google-app-engine,google-cloud-datastore,objectify,Google App Engine,Google Cloud Datastore,Objectify,我有一种数据存储类型,其中ID字段是String ofy().load().type( Scores.class ).id( playerName ).now(); 这获取空值。我已确认具有给定playername的实体存在。 这种情况不会发生在ID长的另一种类型上 分数等级代码 import com.googlecode.objectify.Key; import com.googlecode.objectify.annotation.Entity; import com.googlecod
ofy().load().type( Scores.class ).id( playerName ).now();
这获取空值。我已确认具有给定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;
}
import com.googlecode.objectify.Key;
导入com.googlecode.objectify.annotation.Entity;
导入com.googlecode.objectify.annotation.Id;
导入com.googlecode.objectify.annotation.Parent;
@实体
公开课成绩
{
@母公司
公钥;父密钥;
@身份证
公共字符串播放器名称;
公共价值观;
}
您需要确保为Objectify提供了足够的信息来构造实体的键。因此,如果实体有一个祖先
,那么仅ID/名称是不够的
如果您的实体具有祖先,则可以构造键,然后按键加载实体,如下所示:
Key<Scores> scoreKey = Key.create(parentKey, Scores.class, playerName);
ofy().load().key(scoreKey).now();
Key scoreKey=Key.create(parentKey,Scores.class,playerName);
ofy().load().key(scoreKey.now();
你能给出你的班级分数代码吗?目前,您可以尝试执行plarName.trim()
。一年前,我在一个应用程序中遇到了一些字符串格式的问题。你能看到数据存储中(使用数据存储查看器)存在键名为playerName
的实体,并且它没有祖先吗?@tx802 Bang on!它有一个祖先。我早该料到的。让我尝试在语句中添加祖先()。@Pintouch用代码更新问题。您可以使用key=key单独创建密钥。创建(parentKey,Scores.class,playerName)
,然后调用y().load().key(key)。现在()-这样行吗?