I use Long
ids in my entities, not only to store them in the datastore, but to reference other entities. Now, I'm using RequestFactory to create() objects on the client and persist them, but I need a way to figure out what id the server has generated.
Here's one way I've figured out that requires two trips:
final OrganizationProxy proxy = context.create(OrganizationProxy.class);
context.persist().using(proxy).fire(new Receiver<Void>(){
public void onSuccess(Void response)
{
requestFactory.find(proxy.stableId()).fire(new Receiver<OrganizationProxy>()
{
public void onSuccess(OrganizationProxy response)
{
//hey, now response has the server-generated id in it, along with any other values the server populated
}
});
}
});
But it seems like there must be a way to get the persistent id without the second trip. It seems like requestFactory.find() would need the persistent id to work at all in the first place.
How can I get at the persistent id without a second request to the server?
=======Update=======
It finally occurred to me (after tbroyer told me ;)) that I could return the Long
id from the persist() method in the RequestContext. This doesn't retrieve the persistent id from the EntityProxyId
, but it does get me the persistent id of a new object in a single request.
I'll leave this question open - I am still interested in getting the persistent id out of an EntityProxyId.
The class implementing an EntityProxyId
is SimpleEntityProxyId
. This class has a method getServerId()
, which will return the id. So by checking with instanceof
you can then call the method. (Actually RequestFactory.getHistoryToken()
doesn't even check, but simply casts to this class).
Now the bad news: it's encoded and the base class for SimpleEntityProxyId
, which is SimpleProxyId
and contains the method getServerId()
, specifically states:
The encodedAddress is totally opaque to the client. It's probably a base64-encoded string, but it could be digits of pi. Any code that does anything other than send the contents of this field back to the server is wrong.
(Where the field encodedAddress
contains the server id.)