use of com.google.cloud.datastore.Datastore 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();
}
use of com.google.cloud.datastore.Datastore in project google-cloud-java by GoogleCloudPlatform.
the class TransactionSnippets method isActive.
/**
* Example of verifying if a transaction is active.
*/
// [TARGET isActive()]
public Key isActive() {
Datastore datastore = transaction.getDatastore();
// [START isActive]
// create an entity
KeyFactory keyFactory = datastore.newKeyFactory().setKind("MyKind");
Key key = datastore.allocateId(keyFactory.newKey());
Entity entity = Entity.newBuilder(key).set("description", "active()").build();
// calling transaction.active() now would return true
try {
// add the entity and commit
transaction.put(entity);
transaction.commit();
} finally {
// then transaction.active() will be false
if (transaction.isActive()) {
// otherwise it's true and we need to rollback
transaction.rollback();
}
}
// [END isActive]
return key;
}
use of com.google.cloud.datastore.Datastore in project google-cloud-java by GoogleCloudPlatform.
the class TransactionSnippets method run.
/**
* Example of running a query to find all entities with an ancestor.
*/
// [TARGET run(Query)]
// [VARIABLE "my_parent_key_name"]
public List<Entity> run(String parentKeyName) {
Datastore datastore = transaction.getDatastore();
// [START run]
KeyFactory keyFactory = datastore.newKeyFactory().setKind("ParentKind");
Key parentKey = keyFactory.newKey(parentKeyName);
// Build a query
Query<Entity> query = Query.newEntityQueryBuilder().setKind("MyKind").setFilter(PropertyFilter.hasAncestor(parentKey)).build();
QueryResults<Entity> results = transaction.run(query);
List<Entity> entities = Lists.newArrayList();
while (results.hasNext()) {
Entity result = results.next();
// do something with result
entities.add(result);
}
transaction.commit();
// [END run]
return entities;
}
use of com.google.cloud.datastore.Datastore in project google-cloud-java by GoogleCloudPlatform.
the class TransactionSnippets method addSingleEntity.
/**
* Example of adding a single entity.
*/
// [TARGET add(FullEntity)]
// [VARIABLE "my_key_name"]
public void addSingleEntity(String keyName) {
Datastore datastore = transaction.getDatastore();
// [START addSingleEntity]
Key key = datastore.newKeyFactory().setKind("MyKind").newKey(keyName);
Entity.Builder entityBuilder = Entity.newBuilder(key);
entityBuilder.set("propertyName", "value");
Entity entity = entityBuilder.build();
transaction.add(entity);
transaction.commit();
// [END addSingleEntity]
}
use of com.google.cloud.datastore.Datastore in project google-cloud-java by GoogleCloudPlatform.
the class TransactionSnippets method active.
/**
* Example of verifying if a transaction is active.
*/
// [TARGET active()]
public Key active() {
Datastore datastore = transaction.getDatastore();
// [START active]
// create an entity
KeyFactory keyFactory = datastore.newKeyFactory().setKind("MyKind");
Key key = datastore.allocateId(keyFactory.newKey());
Entity entity = Entity.newBuilder(key).set("description", "active()").build();
// calling transaction.active() now would return true
try {
// add the entity and commit
transaction.put(entity);
transaction.commit();
} finally {
// then transaction.isActive() will be false
if (transaction.isActive()) {
// otherwise it's true and we need to rollback
transaction.rollback();
}
}
// [END active]
return key;
}
Aggregations