Java Code Examples for com.google.appengine.api.datastore.PreparedQuery#asSingleEntity()
The following examples show how to use
com.google.appengine.api.datastore.PreparedQuery#asSingleEntity() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: QueryOptimizationsTest.java From appengine-tck with Apache License 2.0 | 6 votes |
@Test public void testKeysOnly() throws Exception { Entity parent = createTestEntityWithUniqueMethodNameKey("Person", "testKeysOnly"); Key key = parent.getKey(); Entity john = createEntity("Person", key) .withProperty("name", "John") .withProperty("surname", "Doe") .store(); Query query = new Query("Person") .setAncestor(key) .setKeysOnly(); PreparedQuery preparedQuery = service.prepare(query); Entity entity = preparedQuery.asSingleEntity(); assertEquals(john.getKey(), entity.getKey()); assertNull(entity.getProperty("name")); assertNull(entity.getProperty("surname")); }
Example 2
Source File: QueryOptimizationsTest.java From appengine-tck with Apache License 2.0 | 6 votes |
@Test public void testProjectionsWithoutType() throws Exception { String methodName = "testProjectionsWithoutType"; Entity parent = createTestEntityWithUniqueMethodNameKey("Product", methodName); Key key = parent.getKey(); Entity e = createEntity("Product", key) .withProperty("long", 123L) .store(); Query query = new Query("Product") .setAncestor(key) .addProjection(new PropertyProjection("long", null)); PreparedQuery preparedQuery = service.prepare(query); Entity result = preparedQuery.asSingleEntity(); assertEquals(e.getKey(), result.getKey()); RawValue rawValue = (RawValue) result.getProperty("long"); assertEquals(Long.valueOf(123L), rawValue.asType(Long.class)); assertEquals(Long.valueOf(123L), rawValue.asStrictType(Long.class)); }
Example 3
Source File: QueryBasicsTest.java From appengine-tck with Apache License 2.0 | 6 votes |
@Test public void singleEntityThrowsTooManyResultsExceptionWhenMoreThanOneResult() throws Exception { String methodName = "singleEntityThrowsTooManyResultsExceptionWhenMoreThanOneResult"; Key parentKey = createQueryBasicsTestParent(methodName); createEntity("Person", parentKey).store(); createEntity("Person", parentKey).store(); PreparedQuery preparedQuery = service.prepare(new Query("Person")); try { preparedQuery.asSingleEntity(); fail("Expected PreparedQuery.TooManyResultsException"); } catch (PreparedQuery.TooManyResultsException e) { // pass } }
Example 4
Source File: PersistingTest.java From appengine-tck with Apache License 2.0 | 6 votes |
@Test public void putStoresEntity() throws Exception { DatastoreService ds = DatastoreServiceFactory.getDatastoreService(); Entity client = new Entity("Client"); client.setProperty("username", "alesj"); client.setProperty("password", "password"); final Key key = ds.put(client); try { Query query = new Query("Client"); query.setFilter(new Query.FilterPredicate("username", Query.FilterOperator.EQUAL, "alesj")); PreparedQuery pq = ds.prepare(query); Entity result = pq.asSingleEntity(); Assert.assertNotNull(result); Assert.assertEquals(key, result.getKey()); Assert.assertEquals("alesj", result.getProperty("username")); Assert.assertEquals("password", result.getProperty("password")); } finally { ds.delete(key); } }
Example 5
Source File: QueriesTest.java From java-docs-samples with Apache License 2.0 | 5 votes |
private Entity retrievePersonWithLastName(String targetLastName) { // [START gae_java8_datastore_single_retrieval] Query q = new Query("Person") .setFilter(new FilterPredicate("lastName", FilterOperator.EQUAL, targetLastName)); PreparedQuery pq = datastore.prepare(q); Entity result = pq.asSingleEntity(); // [END gae_java8_datastore_single_retrieval] return result; }
Example 6
Source File: QueryTestBase.java From appengine-tck with Apache License 2.0 | 5 votes |
protected void assertSingleResult(Entity expectedEntity, Query query) { PreparedQuery preparedQuery = service.prepare(query); assertEquals("number of results", 1, preparedQuery.countEntities(withDefaults())); Entity entityFromQuery = preparedQuery.asSingleEntity(); assertEquals(expectedEntity, entityFromQuery); }
Example 7
Source File: DatastoreMultitenancyTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test public void testQueryOnSomePropertyWithKeyInDifferentNamespace() { NamespaceManager.set("one"); Key keyInNamespaceOne = KeyFactory.createKey("kind", 1); NamespaceManager.set("two"); Query query = new Query("kind").setFilter(new Query.FilterPredicate("someProperty", EQUAL, keyInNamespaceOne)); PreparedQuery preparedQuery = service.prepare(query); preparedQuery.asSingleEntity(); // should not throw IllegalArgumentException as in previous test preparedQuery.asIterator().hasNext(); // should not throw IllegalArgumentException as in previous test preparedQuery.asList(withDefaults()).size(); // should not throw IllegalArgumentException as in previous test }
Example 8
Source File: QueryOptimizationsTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test public void testProjections() throws Exception { Entity parent = createTestEntityWithUniqueMethodNameKey("Product", "testProjections"); Key key = parent.getKey(); Entity e = createEntity("Product", key) .withProperty("price", 123L) .withProperty("percent", 0.123) .withProperty("x", -0.321) .withProperty("diff", -5L) .withProperty("weight", 10L) .store(); Query query = new Query("Product") .setAncestor(key) .addProjection(new PropertyProjection("price", Long.class)) .addProjection(new PropertyProjection("percent", Double.class)) .addProjection(new PropertyProjection("x", Double.class)) .addProjection(new PropertyProjection("diff", Long.class)); PreparedQuery preparedQuery = service.prepare(query); Entity result = preparedQuery.asSingleEntity(); assertEquals(e.getKey(), result.getKey()); assertEquals(e.getProperty("price"), result.getProperty("price")); assertEquals(e.getProperty("percent"), result.getProperty("percent")); assertEquals(e.getProperty("x"), result.getProperty("x")); assertEquals(e.getProperty("diff"), result.getProperty("diff")); assertNull(result.getProperty("weight")); }
Example 9
Source File: DatastoreHelperTestBase.java From appengine-tck with Apache License 2.0 | 5 votes |
private void assertIAEWhenGettingSingleEntity(PreparedQuery preparedQuery) { try { preparedQuery.asSingleEntity(); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException ex) { // pass } }