Google App Engine Objectify get grandson of entity

122 Views Asked by At

I have entities like this : User->Blague->Score where an user have numerous blagues(childs) and a blague have one score.

I'm trying to get the score from a blague, but when I write this :

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

(The ids are corrects) score is null, unlike blague and userPoster. Why is it null ?

The entities are created like that :

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

and of course, the classes are registered.

How can I get the score from a blague ?

Thanks

Edit : The Socre code :

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

There are 1 best solutions below

0
On BEST ANSWER

You need to be very careful to maintain the key hierarchy.

You state that your intended entity hierarchy is User -> Blague -> Score, but this code below does not do that:

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();

When you create the blagueKey, you do not use the userKey as the parent. While I do not encourage the code style you have written, if you wanted to do so, you need to do this:

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

In general, you should let objectify handle this for you by using Ref<User>, Ref<Blague> as the @Parent objects, or if you must create Key objects, use Key.create(T pojo) rather than managing Keys yourself.