开发者

Generating id in the low level API

开发者 https://www.devze.com 2023-02-18 18:06 出处:网络
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:

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(enti开发者_开发问答ty);

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.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消