当前位置: 首页>>代码示例>>Java>>正文


Java PreparedQuery.asSingleEntity方法代码示例

本文整理汇总了Java中com.google.appengine.api.datastore.PreparedQuery.asSingleEntity方法的典型用法代码示例。如果您正苦于以下问题:Java PreparedQuery.asSingleEntity方法的具体用法?Java PreparedQuery.asSingleEntity怎么用?Java PreparedQuery.asSingleEntity使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.google.appengine.api.datastore.PreparedQuery的用法示例。


在下文中一共展示了PreparedQuery.asSingleEntity方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testKeysOnly

import com.google.appengine.api.datastore.PreparedQuery; //导入方法依赖的package包/类
@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"));
}
 
开发者ID:GoogleCloudPlatform,项目名称:appengine-tck,代码行数:22,代码来源:QueryOptimizationsTest.java

示例2: testProjectionsWithoutType

import com.google.appengine.api.datastore.PreparedQuery; //导入方法依赖的package包/类
@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));
}
 
开发者ID:GoogleCloudPlatform,项目名称:appengine-tck,代码行数:23,代码来源:QueryOptimizationsTest.java

示例3: singleEntityThrowsTooManyResultsExceptionWhenMoreThanOneResult

import com.google.appengine.api.datastore.PreparedQuery; //导入方法依赖的package包/类
@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
    }
}
 
开发者ID:GoogleCloudPlatform,项目名称:appengine-tck,代码行数:17,代码来源:QueryBasicsTest.java

示例4: putStoresEntity

import com.google.appengine.api.datastore.PreparedQuery; //导入方法依赖的package包/类
@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);
    }
}
 
开发者ID:GoogleCloudPlatform,项目名称:appengine-tck,代码行数:21,代码来源:PersistingTest.java

示例5: createSaveObject

import com.google.appengine.api.datastore.PreparedQuery; //导入方法依赖的package包/类
@Test
public void createSaveObject() throws Exception {

  Greeting g = new Greeting("default", TEST_CONTENT);
  ObjectifyService.ofy().save().entity(g).now();

  Query query = new Query("Greeting")
      .setAncestor(new KeyFactory.Builder("Guestbook", "default").getKey());
  PreparedQuery pq = ds.prepare(query);
  Entity greeting = pq.asSingleEntity();    // Should only be one at this point.
  assertEquals(greeting.getProperty("content"), TEST_CONTENT);
}
 
开发者ID:GoogleCloudPlatform,项目名称:java-docs-samples,代码行数:13,代码来源:GreetingTest.java

示例6: doPost_userNotLoggedIn

import com.google.appengine.api.datastore.PreparedQuery; //导入方法依赖的package包/类
@Test
public void doPost_userNotLoggedIn() throws Exception {
  servletUnderTest.doPost(mockRequest, mockResponse);

  Query query = new Query("Greeting")
      .setAncestor(new KeyFactory.Builder("Guestbook", "default").getKey());
  PreparedQuery pq = ds.prepare(query);

  Entity greeting = pq.asSingleEntity();    // Should only be one at this point.
  assertEquals(greeting.getProperty("content"), testPhrase);
}
 
开发者ID:GoogleCloudPlatform,项目名称:java-docs-samples,代码行数:12,代码来源:SignGuestbookServletTest.java

示例7: retrievePersonWithLastName

import com.google.appengine.api.datastore.PreparedQuery; //导入方法依赖的package包/类
private Entity retrievePersonWithLastName(String targetLastName) {
  // [START single_retrieval_example]
  Query q =
      new Query("Person")
          .setFilter(new FilterPredicate("lastName", FilterOperator.EQUAL, targetLastName));

  PreparedQuery pq = datastore.prepare(q);
  Entity result = pq.asSingleEntity();
  // [END single_retrieval_example]
  return result;
}
 
开发者ID:GoogleCloudPlatform,项目名称:java-docs-samples,代码行数:12,代码来源:QueriesTest.java

示例8: createSaveObject

import com.google.appengine.api.datastore.PreparedQuery; //导入方法依赖的package包/类
@Test
public void createSaveObject() throws Exception {

  Greeting g = new Greeting("default", TEST_CONTENT);
  ObjectifyService.ofy().save().entity(g).now();

  Query query =
      new Query("Greeting").setAncestor(new KeyFactory.Builder("Guestbook", "default").getKey());
  PreparedQuery pq = ds.prepare(query);
  Entity greeting = pq.asSingleEntity(); // Should only be one at this point.
  assertEquals(greeting.getProperty("content"), TEST_CONTENT);
}
 
开发者ID:GoogleCloudPlatform,项目名称:java-docs-samples,代码行数:13,代码来源:GreetingTest.java

示例9: doPost_userNotLoggedIn

import com.google.appengine.api.datastore.PreparedQuery; //导入方法依赖的package包/类
@Test
public void doPost_userNotLoggedIn() throws Exception {
  servletUnderTest.doPost(mockRequest, mockResponse);

  Query query =
      new Query("Greeting").setAncestor(new KeyFactory.Builder("Guestbook", "default").getKey());
  PreparedQuery pq = ds.prepare(query);

  Entity greeting = pq.asSingleEntity(); // Should only be one at this point.
  assertEquals(greeting.getProperty("content"), testPhrase);
}
 
开发者ID:GoogleCloudPlatform,项目名称:java-docs-samples,代码行数:12,代码来源:SignGuestbookServletTest.java

示例10: getUserImpl

import com.google.appengine.api.datastore.PreparedQuery; //导入方法依赖的package包/类
private User getUserImpl(String userId) {
    /* TODO: キャッシュを使うようにすること */
    Query.Filter filter = new Query.FilterPredicate("userId", Query.FilterOperator.EQUAL, userId);
    Query query = new Query("User").setFilter(filter);
    DatastoreService service = DatastoreServiceFactory.getDatastoreService();
    PreparedQuery preparedQuery = service.prepare(query);
    Entity entity = preparedQuery.asSingleEntity();

    User user = new User(entity);
    return user;
}
 
开发者ID:nakaken0629,项目名称:iped,代码行数:12,代码来源:UserDomain.java

示例11: assertSingleResult

import com.google.appengine.api.datastore.PreparedQuery; //导入方法依赖的package包/类
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);
}
 
开发者ID:GoogleCloudPlatform,项目名称:appengine-tck,代码行数:8,代码来源:QueryTestBase.java

示例12: testQueryOnSomePropertyWithKeyInDifferentNamespace

import com.google.appengine.api.datastore.PreparedQuery; //导入方法依赖的package包/类
@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
}
 
开发者ID:GoogleCloudPlatform,项目名称:appengine-tck,代码行数:13,代码来源:DatastoreMultitenancyTest.java

示例13: testProjections

import com.google.appengine.api.datastore.PreparedQuery; //导入方法依赖的package包/类
@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"));
}
 
开发者ID:GoogleCloudPlatform,项目名称:appengine-tck,代码行数:30,代码来源:QueryOptimizationsTest.java

示例14: assertIAEWhenGettingSingleEntity

import com.google.appengine.api.datastore.PreparedQuery; //导入方法依赖的package包/类
private void assertIAEWhenGettingSingleEntity(PreparedQuery preparedQuery) {
    try {
        preparedQuery.asSingleEntity();
        fail("Expected IllegalArgumentException");
    } catch (IllegalArgumentException ex) {
        // pass
    }
}
 
开发者ID:GoogleCloudPlatform,项目名称:appengine-tck,代码行数:9,代码来源:DatastoreHelperTestBase.java

示例15: getDevice

import com.google.appengine.api.datastore.PreparedQuery; //导入方法依赖的package包/类
/**
 * Gets device entity for a given player and device id.
 *
 * @param playerKey the entity key of the player.
 * @param deviceId the id of the device to retrieve.
 * @return the retrieved device entity or null if there is no device entity for this playerKey and
 *         deviceId.
 */
private Entity getDevice(Key playerKey, String deviceId) {
  Filter deviceIdFilter =
      new FilterPredicate(DeviceEntity.DEVICEID_PROPERTY, FilterOperator.EQUAL, deviceId);

  Query query = new Query(DeviceEntity.KIND, playerKey).setFilter(deviceIdFilter).setKeysOnly();

  PreparedQuery preparedQuery = dataStore.prepare(query);

  return preparedQuery.asSingleEntity();
}
 
开发者ID:GoogleCloudPlatform,项目名称:solutions-griddler-sample-backend-java,代码行数:19,代码来源:DeviceService.java


注:本文中的com.google.appengine.api.datastore.PreparedQuery.asSingleEntity方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。