Question

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.

Was it helpful?

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.

OTHER TIPS

Perhaps try to use "allocateIds" to allocate a range of Ids to use? This will give you a set of reserved keys to use. I doubt you will be able to get a strict sequence such as in relational databases, but at least you would be able to get guaranteed unique and usable keys.

See the documentation for DatastoreService:

http://code.google.com/appengine/docs/java/javadoc/com/google/appengine/api/datastore/DatastoreService.html#allocateIds%28com.google.appengine.api.datastore.Key,%20java.lang.String,%20long%29

Also for further guidance you might take a look at how Datanucleus uses this API:

http://code.google.com/p/datanucleus-appengine/source/browse/trunk/src/org/datanucleus/store/appengine/valuegenerator/SequenceGenerator.java?r=473

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top