javagoogle-app-enginedatastorelow-level-api

Generating id in the low level API


I have to use the low level API to persist an entity of type Value in Google App Engine. I've been searching and I have only found a examples in this way:

DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Key k = KeyFactory.createKey(Value.class.getSimpleName(), id);
Entity entity = new Entity(k);
entity.setProperty("column1", value.getColumn1());
entity.setProperty("column2", value.getColumn2());
datastore.put(entity);

My problem is that I don't know the id (identifier of Value) in advance because I need it to be generated as a sequence. It would be the way to do it in the low level API as it is done in JDO as:

@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Long id;

How can I retrieve the id in the low level or configure it to be generated as a sequence?

Thanks.


Solution

  • The Entity class has many constructors. Use the one that takes a single string - the kind name - and the ID will be generated for you when you store it in the datastore.