在低级 API 中生成 id
我必须使用低级 API 在 Google App Engine 中保留 Value 类型的实体。我一直在搜索,但只找到了这样的示例:
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);
我的问题是我事先不知道 id (值的标识符),因为我需要将它生成为序列。这将是在低级 API 中执行此操作的方法,就像在 JDO 中执行的那样:
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Long id;
如何在低级中检索 id 或将其配置为作为序列生成?
谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Entity
类有许多构造函数。使用采用单个字符串(种类名称)的字符串,当您将其存储在数据存储中时,将为您生成 ID。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.也许尝试使用“allocateIds”来分配一系列要使用的 Id?这将为您提供一组可供使用的保留密钥。我怀疑您是否能够获得严格的序列,例如在关系数据库中,但至少您将能够获得有保证的唯一且可用的密钥。
请参阅 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
另外,为了获得进一步的指导,您可以查看 Datanucleus 如何使用此 API:
http://code.google.com/p/datanucleus-appengine/source/browse/trunk/src/org/datanucleus/store/appengine/valuegenerator/SequenceGenerator.java?r=473
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