use of com.google.appengine.api.datastore.Key in project java-docs-samples by GoogleCloudPlatform.
the class TransactionsTest method usesForTransactions_readSnapshot.
@Test
public void usesForTransactions_readSnapshot() throws Exception {
String boardName = "my-message-board";
Entity b = new Entity("MessageBoard", boardName);
b.setProperty("count", 13);
datastore.put(b);
// [START uses_for_transactions_3]
DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
// Display information about a message board and its first 10 messages.
Key boardKey = KeyFactory.createKey("MessageBoard", boardName);
Transaction txn = datastore.beginTransaction();
Entity messageBoard = datastore.get(boardKey);
long count = (Long) messageBoard.getProperty("count");
Query q = new Query("Message", boardKey);
// This is an ancestor query.
PreparedQuery pq = datastore.prepare(txn, q);
List<Entity> messages = pq.asList(FetchOptions.Builder.withLimit(10));
txn.commit();
// [END uses_for_transactions_3]
assertWithMessage("board.count").that(count).isEqualTo(13L);
}
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 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 QueriesTest method keyFilterExample_kindless_returnsMatchingEntities.
@Test
public void keyFilterExample_kindless_returnsMatchingEntities() throws Exception {
// Arrange
Entity a = new Entity("Child", "a");
Entity b = new Entity("Child", "b");
Entity c = new Entity("Child", "c");
Entity aa = new Entity("Child", "aa", b.getKey());
Entity bb = new Entity("Child", "bb", b.getKey());
Entity aaa = new Entity("Child", "aaa", bb.getKey());
Entity bbb = new Entity("Child", "bbb", bb.getKey());
Entity adult = new Entity("Adult", "a");
Entity zooAnimal = new Entity("ZooAnimal", "a");
datastore.put(ImmutableList.<Entity>of(a, b, c, aa, bb, aaa, bbb, adult, zooAnimal));
// Act
Key lastSeenKey = bb.getKey();
// [START gae_java8_datastore_kindless_query]
Filter keyFilter = new FilterPredicate(Entity.KEY_RESERVED_PROPERTY, FilterOperator.GREATER_THAN, lastSeenKey);
Query q = new Query().setFilter(keyFilter);
// [END gae_java8_datastore_kindless_query]
// Assert
List<Entity> results = datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
assertWithMessage("query results").that(results).containsExactly(// Ancestor path "b/bb/aaa" is greater than "b/bb".
aaa, // Ancestor path "b/bb/bbb" is greater than "b/bb".
bbb, // Kind "ZooAnimal" is greater than "Child"
zooAnimal, // Key name identifier "c" is greater than b.
c);
}
use of com.google.appengine.api.datastore.Key in project java-docs-samples by GoogleCloudPlatform.
the class QueriesTest method ancestorQueryExample_returnsMatchingEntities.
@Test
public void ancestorQueryExample_returnsMatchingEntities() throws Exception {
// [START gae_java8_datastore_ancestor_query]
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Entity tom = new Entity("Person", "Tom");
Key tomKey = tom.getKey();
datastore.put(tom);
Entity weddingPhoto = new Entity("Photo", tomKey);
weddingPhoto.setProperty("imageURL", "http://domain.com/some/path/to/wedding_photo.jpg");
Entity babyPhoto = new Entity("Photo", tomKey);
babyPhoto.setProperty("imageURL", "http://domain.com/some/path/to/baby_photo.jpg");
Entity dancePhoto = new Entity("Photo", tomKey);
dancePhoto.setProperty("imageURL", "http://domain.com/some/path/to/dance_photo.jpg");
Entity campingPhoto = new Entity("Photo");
campingPhoto.setProperty("imageURL", "http://domain.com/some/path/to/camping_photo.jpg");
List<Entity> photoList = Arrays.asList(weddingPhoto, babyPhoto, dancePhoto, campingPhoto);
datastore.put(photoList);
Query photoQuery = new Query("Photo").setAncestor(tomKey);
// This returns weddingPhoto, babyPhoto, and dancePhoto,
// but not campingPhoto, because tom is not an ancestor
List<Entity> results = datastore.prepare(photoQuery).asList(FetchOptions.Builder.withDefaults());
// [END gae_java8_datastore_ancestor_query]
assertWithMessage("query results").that(results).containsExactly(weddingPhoto, babyPhoto, dancePhoto);
}
Aggregations