Google app engine 谷歌应用引擎Objectify获取实体的孙子

Google app engine 谷歌应用引擎Objectify获取实体的孙子,google-app-engine,google-cloud-endpoints,objectify,endpoints,Google App Engine,Google Cloud Endpoints,Objectify,Endpoints,我有这样的实体:User->Blague->Score,其中一个用户有许多Blague(child),一个Blague有一个分数 我正试着从布拉格那里得到分数,但当我写这篇文章时: User userPoster = ofy().load().type(User.class).id(5066549580791808L).now(); Blague blague = ofy().load().type(Blague.class).parent(userPoster).id(460915274363

我有这样的实体:User->Blague->Score,其中一个用户有许多Blague(child),一个Blague有一个分数

我正试着从布拉格那里得到分数,但当我写这篇文章时:

User userPoster = ofy().load().type(User.class).id(5066549580791808L).now();
Blague blague = ofy().load().type(Blague.class).parent(userPoster).id(4609152743636992L).now();
Score score = ofy().load().type(Score.class).parent(blague).id(5735052650479616L).now();
resp.getWriter().println(userPoster);
resp.getWriter().println(blague);
resp.getWriter().println(score);
(ID已更正)分数为空,与blague和userPoster不同。为什么它是空的

实体的创建方式如下:

@ApiMethod(
        name = "addBlague",
        path = "addBlague",
        httpMethod = ApiMethod.HttpMethod.POST)
public void addBlague(
        @Named("category") EnumCategory category,
        @Named("type") EnumType type,
        @Named("lenght") EnumLenght lenght,
        @Named("keywords") List<String> keywords,
        @Named("text") String text,
        @Named("userId") Long userId){
    Key<User> userKey = Key.create(User.class, userId);
    Blague blague = new Blague(category, type, lenght, text, userKey);
    ofy().save().entity(blague).now();
    Key<Blague> blagueKey = Key.create(Blague.class, blague.getId());
    Score score = new Score(0, 0, blagueKey);
    ofy().save().entity(score).now();
    for (String word : keywords) {
        KeyWord keyword = new KeyWord(word, blagueKey);
        ofy().save().entity(keyword);
    }
}
@ApiMethod(
name=“addBlague”,
path=“addBlague”,
httpMethod=ApiMethod.httpMethod.POST)
公共空间(
@命名(“类别”)枚举类别,
@命名(“类型”)枚举类型,
@命名为(“长度”)枚举长度,
@命名(“关键字”)列表关键字,
@命名(“文本”)字符串文本,
@命名(“用户ID”)长用户ID){
Key userKey=Key.create(User.class,userId);
Blague Blague=新Blague(类别、类型、长度、文本、用户密钥);
of y().save().entity(blague.now();
Key blagueKey=Key.create(Blague.class,Blague.getId());
分数=新分数(0,0,blagueKey);
ofy().save().entity(score).now();
for(字符串字:关键字){
关键字=新关键字(word、blagueKey);
ofy().save().entity(关键字);
}
}
当然,课程是注册的

我怎样才能从blague那里得到分数

谢谢

编辑: Socre代码:

package blagueur;

import java.util.HashMap;

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


@Entity
@Index
@Cache
public class Score {
    @Id
    private Long id;
    private int likes;
    private int dislikes;
    private HashMap<String, Boolean> voters;
    @Parent
    private Key<Blague> blagueKey;

    private Score() {}

    public Score(int likes, int dislikes, Key<Blague> blague) {
        this.likes = likes;
        this.dislikes = dislikes;
        this.blagueKey = blague;
        this.voters = new HashMap<String, Boolean>();
    }

    public Long getId() {
        return id;
    }

    public int getLikes() {
        return likes;
    }

    public int getDislikes() {
        return dislikes;
    }

    public void addVote(Long userId, boolean like){
        voters.put(String.valueOf(userId), like);
        if (like){
            likes++;
        }
        else{
            dislikes++;
        }
    }

    public void removeVote(Long userId){
        String id = String.valueOf(userId);
        boolean like = voters.get(String.valueOf(userId));
        voters.remove(String.valueOf(userId));
        if (like){
            likes--;
        }
        else{
            dislikes--;
        }
    }

    public Key<Blague> getBlagueKey() {
        return blagueKey;
    }
}
packageblagueur;
导入java.util.HashMap;
导入com.googlecode.objectify.Key;
导入com.googlecode.objectify.annotation.Cache;
导入com.googlecode.objectify.annotation.Entity;
导入com.googlecode.objectify.annotation.Id;
导入com.googlecode.objectify.annotation.Index;
导入com.googlecode.objectify.annotation.Parent;
@实体
@索引
@缓存
公开课成绩{
@身份证
私人长id;
私人爱好;
私人不喜欢;
私人投票人;
@母公司
私钥;
私有分数(){}
公共分数(整数喜欢,整数不喜欢,关键blague){
this.likes=喜欢;
这个。不喜欢=不喜欢;
this.blagueKey=blague;
this.voctors=new HashMap();
}
公共长getId(){
返回id;
}
公共int getLikes(){
回报喜欢;
}
public int getDislikes(){
回报厌恶;
}
public void addVote(长用户ID,类似布尔值){
投票者.put(String.valueOf(userId),like);
如果(像){
喜欢++;
}
否则{
不喜欢++;
}
}
public void removeVote(长用户ID){
String id=String.valueOf(userId);
boolean like=vorters.get(String.valueOf(userId));
删除(String.valueOf(userId));
如果(像){
喜欢——;
}
否则{
不喜欢——;
}
}
公钥getBlagueKey(){
返回blagueKey;
}
}

您需要非常小心地维护密钥层次结构

您声明您想要的实体层次结构是User->Blague->Score,但下面的代码没有这样做:

Key<User> userKey = Key.create(User.class, userId);
Blague blague = new Blague(category, type, lenght, text, userKey);
ofy().save().entity(blague).now();
Key<Blague> blagueKey = Key.create(Blague.class, blague.getId());
Score score = new Score(0, 0, blagueKey);
ofy().save().entity(score).now();
Key userKey=Key.create(User.class,userId);
Blague Blague=新Blague(类别、类型、长度、文本、用户密钥);
of y().save().entity(blague.now();
Key blagueKey=Key.create(Blague.class,Blague.getId());
分数=新分数(0,0,blagueKey);
ofy().save().entity(score).now();
创建blagueKey时,不使用userKey作为父项。 虽然我不鼓励使用您编写的代码样式,但如果您想这样做,您需要这样做:

...
Key<Blague> blagueKey = Key.create(userKey, Blague.class, blague.getId());
...
。。。
Key blagueKey=Key.create(userKey,Blague.class,Blague.getId());
...

通常,您应该让objectify通过使用
Ref
Ref
作为
@Parent
对象来为您处理此问题,或者如果您必须创建键对象,请使用
Key.create(T pojo)
而不是自己管理键。

分数
的blague键中是否有
@Parent
?可能会给我们看
Score
code。是的,我在答案中添加了一个对socre代码的编辑