I have the following entity:

@Entity
public class ActionIndex extends EntityObjectAutoID {

    @Parent Ref<Action> action;
    @Id Long id;
    @Index List<Long> receivers;
....
}

I want to retrieve the keys of the latest 10 entities that were added to datastore. But if I use this query:

ofy().load().type(ActionIndex.class).limit(10).keys().list(); 

of course I get the first 10 entities instead of the last 10. According to Objectify's doc:

You can sort on @Id properties if this query is restricted to a Class which has no @Parent. Note that this is only important for descending sorting; default iteration is key-ascending.

You can not sort on @Parent properties.

Since my class has a parent (and I need it to have it), what can I do to get the entities in reverse added order without having to add an extra timestamp property?

有帮助吗?

解决方案

I found out how to do it. In order to sort by key, you just need to use __key__ as property. For filtering there is a convenience method called filterKey, but its equivalent for sorting (orderKey), as far as I can see, does not exist.

ofy().load().type(ActionIndex.class).order("-__key__").limit(10).keys().list();

--- Edit ----

Based on the comments below, it appears the use of a Date property is compulsory.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top