DataStore Key handling

50 views
Skip to first unread message

tonycavanagh1929

unread,
May 17, 2019, 3:53:12 AM5/17/19
to Google App Engine
Every Time I wish to carry out any CRUD operation on a datastore, I have to create a Entity first to get the key.

Entity entity = new Entity("Project", key.getProjectId());
entity = datastoreService.get(entity.getKey());

Is this correct.

George (Cloud Platform Support)

unread,
May 18, 2019, 4:35:03 PM5/18/19
to Google App Engine
Hello Tony, 

Each entity in Datastore has a key that uniquely identifies it. The key consists of the following components:

- The namespace of the entity, which allows for multitenancy
- The kind of the entity, which categorizes it for the purpose of queries
- An identifier for the individual entity, which can be either
   *a key name string
   *an integer numeric ID
- An optional ancestor path locating the entity within the database hierarchy. 

You need a key to retrieve, update or delete an entity, and you need to create an entity to have a key for it. You create a new entity by initializing it and setting its properties. Related detail is to be found on the "Entities, Properties, and Keys" documentation page, as well as Creating, Retrieving, Updating, and Deleting Entities.

tonycavanagh1929

unread,
May 19, 2019, 3:06:13 PM5/19/19
to Google App Engine
Thank you for replying  I understand what the key is, My question was that I have to recreate it every time I want to access the DataSotore. I guess I have to,

George (Cloud Platform Support)

unread,
May 19, 2019, 3:58:13 PM5/19/19
to Google App Engine
Hi Tony, 

In fact, the reply looks somewhat different: once the key was created when you created the entity, you use it to retrieve the entity information whenever needed, so you don't need to recreate this key each time you want to access Datastore: you just reuse it. 

tonycavanagh1929

unread,
May 20, 2019, 3:10:33 AM5/20/19
to Google App Engine
But each case is a atomic, so to not recreate a key, I would need to store it somwhere mapped to a id. The idea is that the user sends CRUD operations via CRUD. For each object, I would just pass an ID. And Id is diiferent to a key. 
So on each new call to the datastore to say update an entity. I would have to use the ID to generate the key. 

Are you saying store  the id and the Key in say memcache on the microservice.

Thanks Tony


On Friday, 17 May 2019 08:53:12 UTC+1, tonycavanagh1929 wrote:

George (Cloud Platform Support)

unread,
May 20, 2019, 6:50:44 PM5/20/19
to Google App Engine
We are on the same wavelength about updating entities, I think. For code samples, you could check the "Entities, Properties, and Keys" page. Your use-case is interesting, but does not require more detail to implement, than already provided. 

This discussion group is oriented more towards general opinions, trends, and issues of general nature touching App Engine and Cloud SQL. For coding and programming architecture, as well as implementing CRUD operations, you may be better served in dedicated forums such as stackoverflow, where experienced programmers are within reach and ready to help. 
Reply all
Reply to author
Forward
0 new messages