我正在尝试通过 Java 中的 Objective 查询 App Engine 数据存储。
我在本地存储了一些虚拟数据,但我无法实现按键排序的结果。
这些是类:
父类:
@Entity
public class Parent
{
@Getter
@Setter
@Id
long id;
@Getter
@Setter
String type;
public Parent() {
}
}
主类
@Entity
@Cache
@Index
public class MainObject
{
@Getter
@Setter
@Id
long id;
@Getter
@Setter
@Unindex
String url;
@Getter
@Setter
Date date;
@Parent
@Getter
@Setter
Key<Parent> type;
public MainObject() {
}
}
问题是我想得到这个查询:
Key<Parent> parent = Key.create(Parent.class, 1);
MainObjectlastUrl = OfyService.ofy().load().type(MainObject.class).ancestor(parent).order("-key").first().now();
这将返回 null。
List<MainObject> list = OfyService.ofy().load().type(MainObject.class).ancestor(parent).order("-key").list();
这将返回一个空列表。
但如果我删除订单查询,我将获得所有实体。
list = OfyService.ofy().load().type(MainObject.class).ancestor(parent).list();
有什么想法吗?
我查看了 Objectify 网页,但找不到太多内容。
提前致谢。
最佳答案
表示键的神奇 Google 字段是 __key__
(每边两个下划线)。这是 GAE 内置的,因此您需要 order("-__key__")
。
Objectify 可以在查询时提供一个 orderKey(boolean)
方法,使这稍微方便一些。如果您将它添加到问题跟踪器,我会实现它。
关于java - 按键排序 - 对象化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23149390/