【问题标题】:Ancestor Query with Objectify not returning results使用 Objectify 的祖先查询不返回结果
【发布时间】:2012-04-23 22:39:22
【问题描述】:

我在 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()));

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

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

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

【问题讨论】:

  • DepartmentPersonnelInfo 类与问题有什么关系?还是应该是轮胎类?
  • 是的,这应该是轮胎类。已更正
  • 这里肯定没有显示更多内容,因为在您的示例中看起来没有任何问题(除了手动输入的各种拼写错误)。尝试从数据存储管理页面在 GQL 中运行等效的祖先查询。
  • 这个查询运行良好:select * from Tire where ANCESTOR is Key('Car',--carid--)
  • 我注意到 objectify 网站上的一个示例,它在查询中使用 .fetch()。我在文档中找不到这个。它有什么作用,是否需要?

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


【解决方案1】:

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

网站上列出的Objectify javadoc实际上是主干版本,这是即将推出的Objectify 4。你需要看的是Objectify 3.1 javadoc:这个版本在查询中有fetch()

另外,@GeneratedValuenot an Objectify annotation

【讨论】:

  • 问题是我传递的是字符串 ID 而不是长 ID...问题已解决。所以,是的,你是对的!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-03
  • 2017-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-26
  • 1970-01-01
相关资源
最近更新 更多