Andrii Liakh Andrii Liakh - 1 month ago 6x
Java Question

Can't get Entity from Datastore by Key

I'm trying to access get the Entity by the Key, But I'm getting the exception every time I try do get it.

String key = en.getKey();

Then key is sent to the other servlet as String like 4644337115725824. And when I'm trying to get Entity by this code

Key key = KeyFactory.createKey("User", req.getParameter("key"));
Entity en = datastore.get(key);

On the last line I have an EntityNotFoundException and 'No entity was found matching the key: User("4644337115725824")'
What's wrong?


I think you might have a serialization/deserialization problem for the key. To serialize a key into a websafe String, use KeyFactory.keyToString(key):

Key key = entity.getKey();
String serializedKey = KeyFactory.keyToString(key);

Later on, when you want to deserialize the key on the other servlet, you can use KeyFactory.stringToKey(string):

Key deserializedKey = KeyFactory.stringToKey(serializedKey);
Entity en = datastore.get(deserializedKey);