Neil
Neil

Reputation: 6039

Objectify does not fetch by String ID

I have a datastore kind in which the ID field is String

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

This fetches null. I have confirmed the entity with the given playername exists. This does not happen to another Kind whose ID is long.

Code for Scores class

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

Upvotes: 0

Views: 658

Answers (1)

tx802
tx802

Reputation: 3564

You need to be sure you are giving Objectify enough information to construct the entity's Key. So, if the entity has an ancestor, the ID/Name alone will be insufficient.

If your entity has an ancestor, you can construct the Key and then load the entity by Key, like this:

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

Upvotes: 1

Related Questions