如何按添加位置从数据存储中获取条目? (Google 应用引擎和 JDO)

发布于 2024-11-18 06:25:48 字数 992 浏览 1 评论 0原文

这是我的例子:
将新条目添加到数据存储 //位置 1
将新条目添加到数据存储 //位置 2
添加新条目到数据存储//位置 3

现在我想获取在位置 2 添加的条目。如何使用 google 应用引擎和 JDO 来完成此操作?

在我看来,一种方法是将 PrimaryKey 作为 Long ID,然后使用 getObjectById。所以长ID将等于数据存储自动存储位置。
显示它的代码:

import javax.jdo.annotations.*;
import javax.jdo.PersistenceManager;
import mypackage.PMF

public class Entry {
  @PrimaryKey
  @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
  private Long ID;

  //.. other fields
}


// create entries (suppose datastore is empty and i don't need deletes in future)
PersistenceManager pm = PMF.get().getPersistenceManager()
for(int i=0; i<10; i++){
  pm.makePersistent(new Entry());
}
// created entries have their IDs exaclty = 1,2,3,4,5,6,7,8,9,10 ?

// fetch entry, that was added second
Key entryKey = KeyFactory.createKey(Entry.class.getSimpleName(), 2L);
Entry secondEntry = pm.getObjectById(Entry.class, entryKey);  

我能否确定长 ID 字段对于新条目总是增加 1,并且对于第一个条目等于 1? 或者还有其他更好的方法? :)

Here is my example:
Add new entry to datastore //position 1
Add new entry to datastore //position 2
Add new entry to datastore //position 3

Now i want to get entry, that was added at position 2. How it can be done with google app engine and JDO?

Looks for me that one way is to give PrimaryKey as Long ID and then use getObjectById. So Long ID will be equal to storing position automatically by the datastore.
Code that shows it:

import javax.jdo.annotations.*;
import javax.jdo.PersistenceManager;
import mypackage.PMF

public class Entry {
  @PrimaryKey
  @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
  private Long ID;

  //.. other fields
}


// create entries (suppose datastore is empty and i don't need deletes in future)
PersistenceManager pm = PMF.get().getPersistenceManager()
for(int i=0; i<10; i++){
  pm.makePersistent(new Entry());
}
// created entries have their IDs exaclty = 1,2,3,4,5,6,7,8,9,10 ?

// fetch entry, that was added second
Key entryKey = KeyFactory.createKey(Entry.class.getSimpleName(), 2L);
Entry secondEntry = pm.getObjectById(Entry.class, entryKey);  

Can i be sure that Long ID field will be always incremented by 1 for new entry and it will equal 1 for first?
Or there is another better way? :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

逐鹿 2024-11-25 06:25:48

“位置”的概念不是 App Engine 的概念。 App Engine 不保证系统生成的 ID 将按顺序无间隙地分配。

请参阅 http://code.google.com/appengine/docs/ java/datastore/entities.html#Kinds_IDs_and_Names

如果您小心的话,您可以自己接管 ID 分配,但是,根据您的应用程序及其使用模式,创建您自己的 id引入了一个瓶颈,将限制您快速创建新实体的能力。

This notion of "position" isn't an App Engine concept. App Engine makes no guarantee that system-generated IDs will be assigned sequentially with no gaps.

See http://code.google.com/appengine/docs/java/datastore/entities.html#Kinds_IDs_and_Names

You can take over ID assignment yourself, if you're careful, but, depending on your app and its usage pattern, creating your own ids introduces a bottleneck that'll limit your ability to create new entities quickly.

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