1

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.

DataNucleus
  • 15,497
  • 3
  • 32
  • 37
Javi
  • 19,387
  • 30
  • 102
  • 135

2 Answers2

1

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.

Nick Johnson
  • 100,655
  • 16
  • 128
  • 198
  • and how will low level API know which my identifier is? Should I add @PrimaryKey even when I don't use JDO – Javi Mar 18 '11 at 19:24
  • @Javi What would you add @PrimaryKey to? The low level API will know because the RPC call returns with the ID, which it updates the created entity with. – Nick Johnson Mar 18 '11 at 21:02
0

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

Brummo
  • 1,030
  • 2
  • 12
  • 18