Search in sources :

Example 1 with Response

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

the class TransactionSnippets method multiplePutEntitiesDeferredId.

/**
   * Example of putting multiple entities with deferred id allocation.
   */
// [TARGET putWithDeferredIdAllocation(FullEntity...)]
public List<Key> multiplePutEntitiesDeferredId() {
    Datastore datastore = transaction.getDatastore();
    // [START multiplePutEntitiesDeferredId]
    IncompleteKey key1 = datastore.newKeyFactory().setKind("MyKind").newKey();
    FullEntity.Builder entityBuilder1 = FullEntity.newBuilder(key1);
    entityBuilder1.set("propertyName", "value1");
    FullEntity entity1 = entityBuilder1.build();
    IncompleteKey key2 = datastore.newKeyFactory().setKind("MyKind").newKey();
    FullEntity.Builder entityBuilder2 = FullEntity.newBuilder(key2);
    entityBuilder2.set("propertyName", "value2");
    FullEntity entity2 = entityBuilder2.build();
    transaction.putWithDeferredIdAllocation(entity1, entity2);
    Response response = transaction.commit();
    // [END multiplePutEntitiesDeferredId]
    return response.getGeneratedKeys();
}
Also used : Response(com.google.cloud.datastore.Transaction.Response) Datastore(com.google.cloud.datastore.Datastore) FullEntity(com.google.cloud.datastore.FullEntity) IncompleteKey(com.google.cloud.datastore.IncompleteKey)

Example 2 with Response

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

the class TransactionSnippets method multipleAddEntitiesDeferredId.

/**
   * Example of adding multiple entities with deferred id allocation.
   */
// [TARGET addWithDeferredIdAllocation(FullEntity...)]
public List<Key> multipleAddEntitiesDeferredId() {
    Datastore datastore = transaction.getDatastore();
    // [START multipleAddEntitiesDeferredId]
    IncompleteKey key1 = datastore.newKeyFactory().setKind("MyKind").newKey();
    FullEntity.Builder entityBuilder1 = FullEntity.newBuilder(key1);
    entityBuilder1.set("propertyName", "value1");
    FullEntity entity1 = entityBuilder1.build();
    IncompleteKey key2 = datastore.newKeyFactory().setKind("MyKind").newKey();
    FullEntity.Builder entityBuilder2 = FullEntity.newBuilder(key2);
    entityBuilder2.set("propertyName", "value2");
    FullEntity entity2 = entityBuilder2.build();
    transaction.addWithDeferredIdAllocation(entity1, entity2);
    Response response = transaction.commit();
    // [END multipleAddEntitiesDeferredId]
    return response.getGeneratedKeys();
}
Also used : Response(com.google.cloud.datastore.Transaction.Response) Datastore(com.google.cloud.datastore.Datastore) FullEntity(com.google.cloud.datastore.FullEntity) IncompleteKey(com.google.cloud.datastore.IncompleteKey)

Aggregations

Datastore (com.google.cloud.datastore.Datastore)2 FullEntity (com.google.cloud.datastore.FullEntity)2 IncompleteKey (com.google.cloud.datastore.IncompleteKey)2 Response (com.google.cloud.datastore.Transaction.Response)2