自动增量列 i JDO、GAE

发布于 2024-10-10 02:15:17 字数 368 浏览 0 评论 0原文

我有一个包含一些字段的数据类,其中一个是我认为是 PK 的 URL,如果我添加一个新项目(进行新的同步)并保存它,如果它是相同的 URL,它应该覆盖数据库中的项目。但我还需要一个“正常”长 id,它针对数据库中的每个对象递增,对于这个对象,除非我将其标记为 PK,否则我总是得到 null,如何才能获得此增量但不将该列作为我的 PK?

@Persistent(valueStrategy=IdGeneratorStrategy.IDENTITY)
private Long _id;
@Persistent
private String _title;
@PrimaryKey
@Persistent
private String _url;

/维克托

I have a data class with some fields, one is a URL that I consider the PK, if I add a new item (do a new sync) and save it it should overwrite the item in the database if it's the same URL. But I also need a "normal" Long id that is incremented for every object in the database and for this one I always get null unless I tags it as a PK, how can a get this incrementation but not have the column as my PK?

@Persistent(valueStrategy=IdGeneratorStrategy.IDENTITY)
private Long _id;
@Persistent
private String _title;
@PrimaryKey
@Persistent
private String _url;

/Viktor

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

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

发布评论

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

评论(1

入怼 2024-10-17 02:15:23

这是可行的:

@PrimaryKey
private String _url;

@Persistent(valueStrategy = IdGeneratorStrategy.SEQUENCE)
private Long _id;

用您的密钥填充 _url 并在保存对象时保留 _id 未设置。然后应该自动填充 _id ,尽管我不确定它是否是一个顺序 ID。

信息来源:Google AppEngine 官方维基

This works:

@PrimaryKey
private String _url;

@Persistent(valueStrategy = IdGeneratorStrategy.SEQUENCE)
private Long _id;

Populate _url with your key and leave _id unset when saving your object. _id should then be automatically populated, though I am not sure whether it is going to be a sequential-id or not.

Information source: official Google AppEngine wiki.

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