use of com.google.cloud.datastore.Datastore 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]
}
use of com.google.cloud.datastore.Datastore 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.Datastore 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.Datastore 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.Datastore in project google-cloud-java by GoogleCloudPlatform.
the class LocalDatastoreHelperTest method testStartStopReset.
@Test
public void testStartStopReset() throws IOException, InterruptedException, TimeoutException {
LocalDatastoreHelper helper = LocalDatastoreHelper.create();
helper.start();
Datastore datastore = helper.getOptions().getService();
Key key = datastore.newKeyFactory().setKind("kind").newKey("name");
datastore.put(Entity.newBuilder(key).build());
assertNotNull(datastore.get(key));
helper.reset();
assertNull(datastore.get(key));
helper.stop(Duration.ofMinutes(1));
thrown.expect(DatastoreException.class);
datastore.get(key);
}
Aggregations