简体   繁体   English

子数据存储区实体的键返回为null

[英]key of child datastore entity returns as null

I have a typical Person-Address entity relationship. 我有一个典型的Person-Address实体关系。 After I query the datastore for Person, I then query Person for the key of address. 在查询数据存储区中的Person后,然后查询Person以获取地址密钥。 The key (ie addrKey , see below) always returns as null. 键(即addrKey ,请参见下文)始终返回为null。 But I look in the datastore, I see both Person and Address entities, with their keys. 但是我看一下数据存储区,看到Person和Address实体,以及它们的键。 So clearly the line Key addrKey = (Key) person.getProperty("address") is not doing what I think it should be doing. 因此,很明显, Key addrKey = (Key) person.getProperty("address")Key addrKey = (Key) person.getProperty("address")并没有执行我认为应该做的事情。 Any ideas how to fix this? 任何想法如何解决这一问题?

DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    Map<Key, Entity> entities = datastore.get(keys);

    List<Person> result = new ArrayList<Person>();
    Iterator<Entry<Key, Entity>> it = entities.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry<Key, Entity> ent = it.next();

        final Entity person = ent.getValue();
        Key key = person.getKey();
        name = (Long) person.getProperty("name");
        Address address = getAddress(datastore, person);

...
    }


private Address getAddress(DatastoreService datastore, Entity person) {
    Key addrKey = (Key) person.getProperty("address");
    try {
        Entity d = datastore.get(addrKey);
        String street = (String) d.getProperty("street");
…
}

My guess is that you persisted address and person at the same time. 我的猜测是您同时保留住地址和个人。 However, address by default has an incomplete key until it is persisted (assuming you're relying on auto id generation for keys). 但是,默认情况下,地址在保留之前具有不完整的密钥(假设您依赖密钥的自动ID生成)。 Therefore, you stored an incomplete key in person. 因此,您亲自存储了一个不完整的密钥。 And fetches using incomplete keys will not work. 并且使用不完整密钥进行的提取将不起作用。

I wrote up a quick test case: 我写了一个快速测试用例:

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;

public class TempTest extends PersistenceTestHelper {

  @Test
  public void testStackOverflow() throws Exception {

    Entity address = new Entity("address", 1);
    address.setProperty("street", "my street");

    Entity person = new Entity("person", 1);
    person.setProperty("addressKey", address.getKey());
    System.out.println(person);

    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    datastore.put(person);
    datastore.put(address);

    Entity personFromDatastore = datastore.get(person.getKey());
    Key addressKey = (Key) personFromDatastore.getProperty("addressKey");
    Entity addressFromDatastore = datastore.get(addressKey);
    System.out.println("fetched street: " + addressFromDatastore.getProperty("street"));

    address = new Entity("address");
    System.out.println("Address with incomplete key: " + address.getKey());
    try {
      addressFromDatastore = datastore.get(address.getKey());
    } catch (IllegalArgumentException e) {
      System.out.println(e);
    }
  }

}

And this is the output: 这是输出:

<Entity [person(1)]:
    addressKey = address(1)
>
fetched street: my street
Address with incomplete key: address(no-id-yet)
java.lang.IllegalArgumentException: address(no-id-yet) is incomplete.

If I'm wrong, it would be nice to understand why you have a try catch in your example. 如果我错了,那么很高兴理解为什么您的示例中有一个try catch。 And also a printout of the Person entity and Address entity at pre-put time and pos-get time would be nice. 而且,在投入前和事后获取时,Person实体和Address实体的打印输出也会很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM