【问题标题】:java - appengnie code to get child entity from datastore not workingjava - 从数据存储中获取子实体的附加代码不起作用
【发布时间】:2012-10-20 03:35:24
【问题描述】:

我有此代码来检查数据存储中是否已存在子实体。问题是它适用于一个父子关系,但总是为另一个关系返回 null。

我的应用中有四种实体 - ORG、Contact、Profile、ProfileOrg。联系人类型实体是 ORG 的子级,而 ProfileOrg 是 Profile 的子级。将联系人添加到 ORG 的逻辑是获取给定组织的所有子实体,然后检查属性。如果属性匹配,则返回实体,否则返回 null。这非常适用于 ORG-Contact 关系。但 Profile-ProfileOrg 关系的完全相同的逻辑不起作用。它总是返回 null。以下是我的代码:

这是获取所有子实体并检查其属性“orgID”是否匹配的代码。

public static Entity getSingleProfileOrg(Key profileKey, String orgID) {

    Iterable<Entity> childProfileOrgs = Util.listChildren("profileOrg",profileKey);

    for (Entity e : childProfileOrgs) {
        if (e.getProperty("orgID").toString() == orgID) {
            return e;
        }
    }
    return null;
}

这是查询数据存储区的代码。

public static Iterable<Entity> listChildren(String kind, Key ancestor) {
  logger.log(Level.INFO, "Search entities based on parent");
  Query query = new Query(kind);
  query.setAncestor(ancestor);
  query.addFilter(Entity.KEY_RESERVED_PROPERTY, FilterOperator.GREATER_THAN, ancestor);
  PreparedQuery pq = datastore.prepare(query);
  return pq.asIterable();
}

请帮忙。这让我陷入了困境。

编辑:为 org 获取 ChildEntities 的代码,效果很好

public static Entity getSingleContact(Key orgKey, String phoneContactID) {
    Iterable<Entity> childContacts = Util.listChildren("contact", orgKey);


    for (Entity e : childContacts) {
        if (e.getProperty("contactID") == phoneContactID) {
            return e;
        }
    }
    return null;
}

编辑:创建或更新 ProfileOrg 的代码

public static void createOrUpdateProfileOrg(String profileID,
        String orgName, String ownerID) {
    String orgID = ownerID + "_" + orgName;
    Entity Profile = Product.getProfile(profileID);
    //Key profileKey = KeyFactory.createKey("Profile", profileID);
    Key profileKey = Profile.getKey();
    Entity profileOrg = getSingleProfileOrg(profileKey, orgID);
    if (profileOrg == null) {
        profileOrg = new Entity("profileOrg", profileKey);
        profileOrg.setProperty("name", orgName);
        profileOrg.setProperty("orgID", orgID);
        profileOrg.setProperty("owner", ownerID);
        profileOrg.setProperty("profileID", profileID);
        Util.persistEntity(profileOrg);
    }

}

线

Entity Profile = Product.getProfile(profileID);

应该可以正常工作,因为它也用于创建配置文件,我没有发现任何问题。

因为我总是从 getSingleProfileOrg(profileKey, orgID);我最终得到了重复的子实体。

【问题讨论】:

    标签: google-app-engine google-cloud-datastore


    【解决方案1】:

    由于在listChildren 中,您想基于祖先进行查询,因此您的方法是错误的。应该是这样的:

    public static Iterable<Entity> listChildren(String kind, Key ancestor) {
      logger.log(Level.INFO, "Search entities based on parent");
      Query query = new Query(kind);
      query.setAncestor(ancestor);
      PreparedQuery pq = datastore.prepare(query);
      return pq.asIterable();
    }
    

    希望这会有所帮助。

    【讨论】:

    • 我检查过,还是一样。那是行不通的。:(当代码逻辑完全相同时,我不明白为什么它适用于一种实体而不适用于另一种实体。
    • 您可以添加您指定模型的代码吗?因为那里可能有什么可疑的东西......
    • 我对此很陌生,代码只是来自我修改过的示例。我不明白你所说的模型是什么意思。我正在添加用于创建或更新 profileOrg 的其他方法。
    • 模型是指您尝试使用此方法查询的类。由于查询是正确的,问题可能出在你构造类的方式上。
    • 没有灭霸。那没有用。我正在使用 profileOrg 来减少数据库的搜索时间,但我知道我对数据存储写入的数量有限制。所以我现在跳过了这个想法。感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-01
    • 1970-01-01
    • 2018-04-20
    • 1970-01-01
    相关资源
    最近更新 更多