Viktor
Viktor

Reputation: 486

Auto increment column i JDO, GAE

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

Upvotes: 1

Views: 2018

Answers (1)

rodion
rodion

Reputation: 15029

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.

Upvotes: 3

Related Questions