Reputation: 19
How do you generate an Id in App engine datastore automatically? I am using the automatically created Backend Function insertEntity().
As primary key I am using
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Long Id;
And when I call the insert function I would like that the datastore automatically assigns this Id...
Thanks.
Upvotes: 1
Views: 962
Reputation: 8806
Technically, there is nothing wrong with what you have done. The ID will get generated automatically.
The problem I believe is the Auto Generated code that has been generated for the insertEntity() method. The code currently expects that the ID value has been provided and based on that, it first tries to retrieve an existing Entity. Since the ID is most likely null i.e. you have not passed it, it is resulting in an exception in the code.
Modify the code to check if the getId() != null
. If not null, only then allow it to go forward, else just invoke the makePersistent()
code.
Upvotes: 1