简体   繁体   中英

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.

    datastore.put(en);
    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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM