Search in sources :

Example 6 with FullEntity

use of com.google.cloud.datastore.FullEntity in project google-cloud-java by GoogleCloudPlatform.

the class TransactionSnippets method multipleAddEntities.

/**
   * Example of adding multiple entities.
   */
// [TARGET add(FullEntity...)]
// [VARIABLE "my_key_name1"]
// [VARIABLE "my_key_name2"]
public void multipleAddEntities(String keyName1, String keyName2) {
    Datastore datastore = transaction.getDatastore();
    // [START multipleAddEntities]
    Key key1 = datastore.newKeyFactory().setKind("MyKind").newKey(keyName1);
    Entity.Builder entityBuilder1 = Entity.newBuilder(key1);
    entityBuilder1.set("propertyName", "value1");
    Entity entity1 = entityBuilder1.build();
    Key key2 = datastore.newKeyFactory().setKind("MyKind").newKey(keyName2);
    Entity.Builder entityBuilder2 = Entity.newBuilder(key2);
    entityBuilder2.set("propertyName", "value2");
    Entity entity2 = entityBuilder2.build();
    transaction.add(entity1, entity2);
    transaction.commit();
// [END multipleAddEntities]
}
Also used : FullEntity(com.google.cloud.datastore.FullEntity) Entity(com.google.cloud.datastore.Entity) Datastore(com.google.cloud.datastore.Datastore) Key(com.google.cloud.datastore.Key) IncompleteKey(com.google.cloud.datastore.IncompleteKey)

Example 7 with FullEntity

use of com.google.cloud.datastore.FullEntity in project google-cloud-java by GoogleCloudPlatform.

the class TransactionSnippets method multiplePutEntities.

/**
   * Example of putting multiple entities.
   */
// [TARGET put(FullEntity...)]
// [VARIABLE "my_key_name1"]
// [VARIABLE "my_key_name2"]
public void multiplePutEntities(String keyName1, String keyName2) {
    Datastore datastore = transaction.getDatastore();
    // [START multiplePutEntities]
    Key key1 = datastore.newKeyFactory().setKind("MyKind").newKey(keyName1);
    Entity.Builder entityBuilder1 = Entity.newBuilder(key1);
    entityBuilder1.set("propertyName", "value1");
    Entity entity1 = entityBuilder1.build();
    Key key2 = datastore.newKeyFactory().setKind("MyKind").newKey(keyName2);
    Entity.Builder entityBuilder2 = Entity.newBuilder(key2);
    entityBuilder2.set("propertyName", "value2");
    Entity entity2 = entityBuilder2.build();
    transaction.put(entity1, entity2);
    transaction.commit();
// [END multiplePutEntities]
}
Also used : FullEntity(com.google.cloud.datastore.FullEntity) Entity(com.google.cloud.datastore.Entity) Datastore(com.google.cloud.datastore.Datastore) Key(com.google.cloud.datastore.Key) IncompleteKey(com.google.cloud.datastore.IncompleteKey)

Example 8 with FullEntity

use of com.google.cloud.datastore.FullEntity in project google-cloud-java by GoogleCloudPlatform.

the class ITDatastoreTest method testGet.

@Test
public void testGet() {
    Entity entity = DATASTORE.get(KEY3);
    assertNull(entity);
    entity = DATASTORE.get(KEY1);
    assertEquals(ENTITY1, entity);
    StringValue value1 = entity.getValue("str");
    assertEquals(STR_VALUE, value1);
    BooleanValue value2 = entity.getValue("bool");
    assertEquals(BOOL_VALUE, value2);
    ListValue value3 = entity.getValue("list");
    assertEquals(LIST_VALUE2, value3);
    TimestampValue value4 = entity.getValue("date");
    assertEquals(TIMESTAMP_VALUE, value4);
    LatLngValue value5 = entity.getValue("latLng");
    assertEquals(LAT_LNG_VALUE, value5);
    FullEntity<IncompleteKey> value6 = entity.getEntity("partial1");
    assertEquals(PARTIAL_ENTITY1, value6);
    ListValue value7 = entity.getValue("emptyList");
    assertEquals(EMPTY_LIST_VALUE, value7);
    assertEquals(7, entity.getNames().size());
    assertFalse(entity.contains("bla"));
}
Also used : FullEntity(com.google.cloud.datastore.FullEntity) Entity(com.google.cloud.datastore.Entity) ProjectionEntity(com.google.cloud.datastore.ProjectionEntity) LatLngValue(com.google.cloud.datastore.LatLngValue) BooleanValue(com.google.cloud.datastore.BooleanValue) ListValue(com.google.cloud.datastore.ListValue) TimestampValue(com.google.cloud.datastore.TimestampValue) StringValue(com.google.cloud.datastore.StringValue) IncompleteKey(com.google.cloud.datastore.IncompleteKey) Test(org.junit.Test)

Aggregations

FullEntity (com.google.cloud.datastore.FullEntity)8 IncompleteKey (com.google.cloud.datastore.IncompleteKey)8 Datastore (com.google.cloud.datastore.Datastore)6 Entity (com.google.cloud.datastore.Entity)6 Key (com.google.cloud.datastore.Key)4 ProjectionEntity (com.google.cloud.datastore.ProjectionEntity)2 Response (com.google.cloud.datastore.Transaction.Response)2 Test (org.junit.Test)2 BooleanValue (com.google.cloud.datastore.BooleanValue)1 DatastoreException (com.google.cloud.datastore.DatastoreException)1 LatLngValue (com.google.cloud.datastore.LatLngValue)1 ListValue (com.google.cloud.datastore.ListValue)1 StringValue (com.google.cloud.datastore.StringValue)1 TimestampValue (com.google.cloud.datastore.TimestampValue)1