GAE Objectify如何将查询结果用于另一个查询

时间:2015-08-21 11:38:06

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

这个问题的基本思路如下。我执行一些查询

      List<Class2> class2entities = ObjectifyService.ofy()
          .load()
          .type(Class2.class) 
          .filter(?,?)    
          .list();

我可以用什么方式执行基于class2entities的另一个查询?

实际上我正在开发基于Objectify的GAE应用程序。我使用以下实体

问题:

 @Entity
public class Problem {
  @Id public String problemname;

  public Problem (String name) {
      problemname = name;
  }

  public Problem () {
      problemname = "nullProblem";
  }
}

元组:

    @Entity
public class Tuple {
  @Parent Ref<Problem> theProblem;
  @Index public String tuple_id;
  @Id public Long id;

public Tuple()
{
    String s="empty operator";
}

public Tuple(String sid, Problem problem)
{
    tuple_id = sid;
    try {
         theProblem = Ref.create(problem);
    }
    catch (java.lang.NullPointerException e)
    {
        System.out.println(e.toString() + " tuple in datastore was not created because of Problem is empty" );
    }
}
}

属性:

@Entity
public class Attribute {
  @Parent com.googlecode.objectify.Ref<Problem> theProblem;
  @Id public Long id;

  public String attributeName;
  @Index public String attributeFieldName;
  @Index public Date date;

  /**
   * Simple constructor just sets the date
   **/
  public Attribute() {
    date = new Date();
  }

  /**
   * A connivence constructor
   **/
  public Attribute(Problem problem, String attributeName) {
    this();
    if( problem != null ) {
      theProblem = Ref.create(problem);  // Creating the Ancestor key
    } else {
      theProblem = Ref.create(new Problem("nullProblem"));
    }
    this.attributeName = attributeName;
  }

  /**
   * Takes all important fields
   **/
  public Attribute(Problem problem, String attributeName, String var_attributeFieldName) {
    this(problem, attributeName);
    attributeFieldName = var_attributeFieldName;
  }

}

CategorizedData:

@Entity
public class CategorizedData {
  @Load public Ref<Attribute> theAttribute;
  @Id Long id;
  @Parent public Key<Tuple> theTuple;
  public String attributeValue;

  @Index public Date date;


  /**
   * getter and setter for theAttribute
   **/

    public Attribute getAttribute() { return theAttribute.get(); }
    public void setAttribute(Attribute attribute) { theAttribute = Ref.create(attribute); }

  /**
   * Simple constructor just sets the date
   **/
  public CategorizedData() {
    date = new Date();
  }

  /**
   * A connivence constructor
   **/
  public CategorizedData(String tupleId, String attribute_field_name, String var_attributeValue) {
    this();
    Attribute attribute = ofy().load().type(Attribute.class).filter("attributeFieldName", attribute_field_name).first().now();
    Tuple tuple = ofy().load().type(Tuple.class).filter("tuple_id",tupleId).first().now();

    if( tupleId != null ) {
      theTuple = Key.create(Tuple.class, tuple.id);          // Creating the Ancestor key
    } else {
      theTuple = Key.create(Tuple.class, (new Tuple()).id);
    }
    if( attribute != null ) {
      theAttribute = Ref.create(attribute);  // Creating the Ancestor ref
    } else {
      theAttribute = Ref.create(new Attribute());
    }
    this.attributeValue = var_attributeValue;
  }

}

现在我想获得给定问题的所有Tuple实体,并使用给定AttributeFieldName的theAttribute字段获取CategorizedData的所有实体。

我需要做类似

的事情
Key<Problem> theProblem = Key.create(Problem.class, problemName);
    // Run an ancestor query 
      List<Tuple> tuples = ObjectifyService.ofy()
          .load()
          .type(Tuple.class) 
          .ancestor(theProblem)    
          .list();

然后我需要在这个列表元组中获取CategorizedData的实体。 我该怎么办?是否可以使用Objectify查询而不是所有数据存储区而是先前查询的结果?请帮帮我......

0 个答案:

没有答案