Sadegh
Sadegh

Reputation: 80

How to store class with sub class by Google Cloud Datastore JSON API?

For storing into GCD, I want to use Google Cloud Datastore JSON API but I couldn't write correct Json request body for object with another class member. Consider I have 2 classes :

public class Foo {
    private String id;
    private Bar bar;
    // getter and setter...
}

public class Bar {
    private String name;
    private String pass;
    // getter and setter...
}

Then I create an object of Foo class that contain an object of Bar class. So I want to insert this object to Cloud Datastore. I wrote this request body :

{
  "transaction":"some bytes",
  "mutation":{
    "insert":[
      {
        "key":{
          "partitionId":{
            "datasetId":"s~my-dataset-id"
          },
          "path":[
            {
              "kind":"Foo",
              "name":"id"
            }
          ]
        },
        "properties":{
          "bar":{
            "entityValue":{
              "name":{
                "stringValue":"Jack"
              },
              "pass":{
                "stringValue":"1234"
              }
            },
            "indexed":false
          },
          "id":{
            "stringValue":"id"
          }
        }
      }
    ]
  }
}

Then all fields except bar will save to cloud datastore. I used "entityValue" but it seems that I should include the entire entity structure (thanks Adam for mention it). But I don't need it as another entity and obviously I shouldn't use "entityValue". So how should I change the request body to insert such an object?

By the way, I can insert a Bar object (but not Foo) by following request :

   {
  "transaction":"some bytes",
  "mutation":{
    "insert":[
      {
        "key":{
          "partitionId":{
            "datasetId":"s~my-project-id"
          },
          "path":[
            {
              "kind":"Bar",
              "name":"John"
            }
          ]
        },
        "properties":{
          "pass":{
            "stringValue":"1234"
          },
          "name":{
            "stringValue":"John"
          }
        }
      }
    ]
  }
}

This is the related link : Related link

Upvotes: 1

Views: 1594

Answers (2)

Patrick Costello
Patrick Costello

Reputation: 3626

You may include Bar as an entityValue. This value looks like a normal Entity, but it doesn't need to have a key. This entity value does not need to exist separately in the Datastore.

For example:

{
  "mutation":{
    "insert":[
      {
        "key":{
          "path":[
            {
              "kind":"Foo",
              "name":"id"
            }
          ]
        },
        "properties":{
          "bar":{
            "entityValue":{
              "properties": {
                "name":{
                  "stringValue":"Jack"
                },
                "pass":{
                  "stringValue":"1234"
                }
              }
            },
            "indexed":false
          },
          "id":{
            "stringValue":"id"
          }
        }
      }
    ]
  },
  "mode": "NON_TRANSACTIONAL"
}

Also as a side note, notice that the key for Foo here has left out the partitionId. This is the preferred thing to do, Datastore will fill in the correct partitionId for you.

Upvotes: 1

Adam
Adam

Reputation: 6015

You have to include the entire entity structure for 'entityValue' (including the "key" and "properties" fields). This is because you are referencing a separate entity in the Datastore and not just another data structure embedded in the current one. See:

Datasets: Entity Format
https://cloud.google.com/datastore/docs/apis/v1beta2/entity

The field 'entityValue": (Entity)' is a clue that you need to start from the beginning of this same entity representation for a new entity.

Upvotes: 0

Related Questions