1
votes

Earlier, I created my entities this way: Entity entity = new Entity("Person", personName);. Which means the ID/Name of this entity will be personName.

Now, I've decided to use Objectify. So I've created a Person.java class to represent this entity.

My question: how can I retrieve the key (either a com.google.appengine.api.datastore.Key or the raw string representation) of the entity using Objectify? Previously, I was able to simply do a personEntity.getKey(). But I'm still trying to figure out how to do that with Objectify after using it to wrap around the Low Level Datastore API.

1
Why do you want a raw DS Key? The whole idea of Objectify is to hide the Datastore so you can deal with POJOs that have e.g. a Long id. Also makes things easier to test compared to using the classes in the c.g.appengine.api.datastore package.Philipp Reichart
Objectify was implemented at a later stage of the project, and before that, we were simply using the Low Level API. (a bad design decision) So there are some methods which are dependent on the raw DS Key.tommi
I use Ofy instead of low-level API, but still find myself often using raw DS Key type. The type-safety of ofy's Key<> comes at a cost in flexibility. Yes, I use long id or String name most of the time, but use raw Key too.Tom

1 Answers

0
votes

Objectify's Key<?> has a method getRaw() which will get you the low-level Key. Check the javadocs.

If you are using the latest 4.0 code, try Key.create(yourEntityPOJO).getRaw().