【问题标题】:List Children of null parent?列出空父母的孩子?
【发布时间】:2012-12-06 14:55:53
【问题描述】:

使用 Objectify 可以查询对象的子对象。一个对象也可以有一个空父对象。

Parent parent;
List<Children> children = ofy().query(Children.class).ancestor(parent).list();

我想知道的是,如果我可以查询一个 null Parent 吗?所以我想知道所有有一个空父母的孩子。如果我在上面传递 null 我会得到一个异常。

同样使用 null propValue 通过属性创建查询不会返回任何内容。

  Query<T> q = ofy().query(clazz);
  q.filter(propName, propValue);
  return q.list();

【问题讨论】:

    标签: google-app-engine objectify


    【解决方案1】:

    如果将 null 传递给祖先()会从 GAE 代码中引发异常(我相信确实如此),那么是的,这是 appengine 的限制。

    请注意,Query 对象是不可变的,因此在您的第二个示例中,filter() 调用无效。您必须重新分配 q 变量:

    q = q.filter(...
    

    在回答如何获取所有具有 null 父级的实体时,您可以在键上使用不等式过滤器:

    Key<Parent> firstParent = Key.create(Parent.class, 1L);  // first possible parent value
    List<Children> children = ofy().query(Children.class).filterKey("<", firstParent).list();
    

    【讨论】:

    • 由于您使用的是 ofy(),我假设您使用的是 Objectify4,它具有 filterKey()。
    • 好的,我已经升级到 Objectify 4。感谢您的帮助。
    • 所以我尝试了你的建议,但有一点变化。但是我的结果列表是空的... Key firstParent = Key.create(Parent.class, 1L); List children = ofy().load().type(Children.class).filterKey("
    猜你喜欢
    • 2012-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-26
    • 1970-01-01
    • 2018-01-15
    • 1970-01-01
    相关资源
    最近更新 更多