在低级 API 中生成 id

发布于 2024-10-24 15:20:21 字数 651 浏览 1 评论 0原文

必须使用低级 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

面如桃花 2024-10-31 15:20:21

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.

旧街凉风 2024-10-31 15:20:21

也许尝试使用“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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文