tejashree mahadik
tejashree mahadik

Reputation: 315

get auto-generated id (objectify datastore)

i want to pass autogenerated id to 'Name/id' at datastore.can anybody help me for this? here is my code :

String Id = "" // i want autogenerated value

profile = new Profile(Id, displayName, mainEmail);

Id must be autogenerated here. so how to pass it?

Upvotes: 1

Views: 458

Answers (1)

konqi
konqi

Reputation: 5227

ObjectifyFactory has a method #allocateId(). You ca find an example usage in this question.

Essentially you do

new ObjectifyFactory().allocateId(Profile.class).getId()

In case you do not need the id right away I would not use this approach. Just annotate the id with @Id, set it to null and save the entity. When you do a ofy().save().entity(...).now() it will return a Key that contains the new id.

Allocating ids via allocated id still performs a datastore request. It will allocate a block of ids of which you will use just one in this case. Use it if you must, don't, if you don't have to.

Upvotes: 3

Related Questions