use of com.google.cloud.datastore.Key in project google-cloud-java by GoogleCloudPlatform.
the class TransactionSnippets method rollback.
/**
* Example of rolling back a transaction.
*/
// [TARGET rollback()]
public Key rollback() {
Datastore datastore = transaction.getDatastore();
// [START rollback]
// create an entity
KeyFactory keyFactory = datastore.newKeyFactory().setKind("MyKind");
Key key = datastore.allocateId(keyFactory.newKey());
Entity entity = Entity.newBuilder(key).set("description", "rollback()").build();
// add the entity and rollback
transaction.put(entity);
transaction.rollback();
// [END rollback]
return key;
}
use of com.google.cloud.datastore.Key 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]
}
use of com.google.cloud.datastore.Key in project google-cloud-java by GoogleCloudPlatform.
the class TransactionSnippets method commit.
/**
* Example of committing a transaction.
*/
// [TARGET commit()]
public Key commit() {
Datastore datastore = transaction.getDatastore();
// [START commit]
// create an entity
KeyFactory keyFactory = datastore.newKeyFactory().setKind("MyKind");
Key key = datastore.allocateId(keyFactory.newKey());
Entity entity = Entity.newBuilder(key).set("description", "commit()").build();
// add the entity and commit
try {
transaction.put(entity);
transaction.commit();
} catch (DatastoreException ex) {
// handle exception
}
return key;
}
use of com.google.cloud.datastore.Key in project google-cloud-java by GoogleCloudPlatform.
the class ITDatastoreSnippets method testAllocateIdSingle.
@Test
public void testAllocateIdSingle() {
Key key = datastoreSnippets.allocateIdSingle();
assertNotNull(key);
}
use of com.google.cloud.datastore.Key in project google-cloud-java by GoogleCloudPlatform.
the class ITQuerySnippets method beforeClass.
@BeforeClass
public static void beforeClass() {
datastore = DatastoreOptions.getDefaultInstance().getService();
Key key1 = Key.newBuilder(datastore.getOptions().getProjectId(), KIND, "key1").build();
Key key2 = Key.newBuilder(datastore.getOptions().getProjectId(), KIND, "key2").build();
entity1 = Entity.newBuilder(key1).set("description", "entity1").build();
entity2 = Entity.newBuilder(key2).set("description", "entity2").build();
datastore.put(entity1, entity2);
}
Aggregations