Reputation: 35282
Based on the Q&A from here: Get an Objectify Entity's Key
For a persisted object, getting the entity key with:
@Transient
Key<Categoria> getKey() {
return new Key<Categoria>(Categoria.class, id);
}
Doensn't return the same key with:
Objectify ofy = ObjectifyService.begin();
Key<Categoria> key = ofy.getFactory().getKey(someobject);
Or should it?
My model looks like this:
@Entity
class Categoria{
@Parent
private Key<Someclass> parentKey;
@Transient
Key<Categoria> getKey() {
return new Key<Categoria>(Categoria.class, id);
}
// Code omitted
}
Upvotes: 0
Views: 180
Reputation: 3639
It should. I always get Objectify Entities via keys created from the a long id. You can also use the returned key to get the long id from the key if needed.
EDIT: You can't get the key the way you are trying.
You have to do.
Key<Car> rootKey = new Key<Car>(Car.class, 959);
Key<Car> keyWithParent = new Key<Car>(parent, Car.class, 959);
So for this line: Key key = ofy.getFactory().getKey(someobject);
key will consist of both the parent key PLUS the Categoria key
that means you have to include the parent key when you do the lookup in your function
Key<Categoria> getKey() {
return new Key<Categoria>(parentKey, Categoria.class, id);
}
Upvotes: 0
Reputation: 13556
It will only produce a different key if Categoria
has a @Parent
field. In that case, you need to pass the parent key into the Key constructor along with the class and id.
Upvotes: 2