use of com.google.appengine.api.datastore.Key in project java-docs-samples by GoogleCloudPlatform.
the class TransactionsTest method usingTransactions.
@Test
public void usingTransactions() throws Exception {
Entity joe = new Entity("Employee", "Joe");
datastore.put(joe);
// [START using_transactions]
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Transaction txn = datastore.beginTransaction();
try {
Key employeeKey = KeyFactory.createKey("Employee", "Joe");
Entity employee = datastore.get(employeeKey);
employee.setProperty("vacationDays", 10);
datastore.put(txn, employee);
txn.commit();
} finally {
if (txn.isActive()) {
txn.rollback();
}
}
// [END using_transactions]
}
use of com.google.appengine.api.datastore.Key in project java-docs-samples by GoogleCloudPlatform.
the class EntitiesTest method deletingAnEntity_deletesAnEntity.
@Test
public void deletingAnEntity_deletesAnEntity() throws Exception {
Entity employee = new Entity("Employee", "asalieri");
datastore.put(employee);
Key employeeKey = KeyFactory.createKey("Employee", "asalieri");
// [START deleting_an_entity]
// Key employeeKey = ...;
datastore.delete(employeeKey);
try {
Entity got = datastore.get(employeeKey);
fail("Expected EntityNotFoundException");
} catch (EntityNotFoundException expected) {
assertWithMessage("exception key name").that(expected.getKey().getName()).isEqualTo("asalieri");
}
}
use of com.google.appengine.api.datastore.Key in project java-docs-samples by GoogleCloudPlatform.
the class EntitiesTest method creatingAnEntity_withoutKeyName_writesEntity.
@Test
public void creatingAnEntity_withoutKeyName_writesEntity() throws Exception {
Key employeeKey = writeEmptyEmployee();
// [START retrieving_an_entity]
// Key employeeKey = ...;
Entity employee = datastore.get(employeeKey);
// [END retrieving_an_entity]
assertWithMessage("retrieved key ID").that(employee.getKey().getId()).isEqualTo(employeeKey.getId());
}
use of com.google.appengine.api.datastore.Key in project java-docs-samples by GoogleCloudPlatform.
the class EntitiesTest method keyFactoryBuilder_makeKeyWithParents.
@Test
public void keyFactoryBuilder_makeKeyWithParents() {
Key greatKey = KeyFactory.createKey("Person", "GreatGrandpa");
Key grandKey = KeyFactory.createKey(greatKey, "Person", "Grandpa");
Key dadKey = KeyFactory.createKey(grandKey, "Person", "Dad");
Key meKey = KeyFactory.createKey(dadKey, "Person", "Me");
// [START generating_keys_2]
Key k = new KeyFactory.Builder("Person", "GreatGrandpa").addChild("Person", "Grandpa").addChild("Person", "Dad").addChild("Person", "Me").getKey();
// [END generating_keys_2]
assertThat(k).isEqualTo(meKey);
}
use of com.google.appengine.api.datastore.Key in project java-docs-samples by GoogleCloudPlatform.
the class GuestbookStrong method listGreetingEntities.
@Override
protected List<Entity> listGreetingEntities(DatastoreService datastore) {
Key guestbookKey = KeyFactory.createKey("Guestbook", guestbookName);
Query query = new Query("Greeting", guestbookKey).setAncestor(guestbookKey).addSort("date", Query.SortDirection.DESCENDING);
return datastore.prepare(query).asList(FetchOptions.Builder.withLimit(10));
}
Aggregations