App Engine ID Key versus Id

To identify JDO objects in the Google App Engine, I use the Key type. It works fine, but when I need to pass this via urls, it gets long.

For example: http://mysite.com/user/aghtaWx1LWFwcHIZCxIGTXlVc2VyGAMMCxIHTXlJbWFnZRgHDA

When viewing my objects in my view manager, I see that the data store also sets an "id" for my entity object, which seems to be an incremental numeric value that is quite short compared to the Key string. Can I use this to capture information about my object? How can I do it? I tried using getObjectbyId() with an identifier instead of a key ... it does not work.

Any ideas?

+7
source share
2 answers

Yes you can do it. Whenever you need to get an identifier, you can use the following method call. Suppose you are using a User entity class object named User : user.getKey().getId() . The identifier is of type long . For more information, see JavaDoc com.google.appengine.api.datastore.Key .

Whenever you have an ID, you can build a Key from it and then just query the object.

 Key key = KeyFactory.createKey("User", id); DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); User user = datastore.get(key); 
+9
source

You need to define the identifier in your Entity as the primary key:

 private class MyObject implements Serializable{ @PrimaryKey @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) private Long id; } 

Then you can try the following:

 long id = someObject.getId(); MyObject mo = getPM().getObjectById(MyObject.class, id); 
+2
source

All Articles