使用Objectify的祖先查询不返回结果

时间:2012-04-11 15:28:22

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

我在appengine上使用objectify 3.1并尝试进行祖先查询。我想获取一个特定类型的对象的所有子节点。以下是我的代码:

@Cached
@Entity
public class Car {

 @Id
 @GeneratedValue(strategy = GenerationType.IDENTITY)
 private Long id;

 private String make;
 private String model;

}

@Cached
@Entity
public class Tire {

@Id
public String keyName; 


@Parent Key<Car> car;

private String brand;
private String size;

}

我的查询是这段代码:

        List<Tire> list= ofy.query(Tire.class).ancestor(new    Key<Car(Car.class,carID))).list();

当我创建轮胎对象时,我使用此代码来设置关系:

        newTire.setCar(new Key<Car>(Car.class,Car.getID()));

我知道父关系存在,因为我可以在数据存储区管理员中查询它,它在解码的实体键中显示父关系:

Decoded entity key: Car: id=135172 > Tire: name=myCarUniqueID

此查询总是返回0结果,似乎我已经遵循了objectify网站上的所有最佳实践。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:2)

您的代码看起来没问题,因此唯一可能出错的是不存在的carID

网站上列出的Objectify javadoc实际上是针对主干版本的,这是即将发布的Objectify 4.您需要查看的是Objectify 3.1 javadoc:此版本在查询中有fetch()

此外,@GeneratedValuenot an Objectify annotation

相关问题