is it possible in Objectify to order by a field of a referenced object?
Example:
public class Foo {
@Id private long id;
@Index Ref<AnObject> ref;
}
public class AnObject {{
@Id private long id;
@Index name;
}
Then I try to do this query and it's not working:
Query<Foo> query = ofy().load().type(Foo.class)
.limit(Constants.DATASTORE_LIMIT)
.order("ref.name");
Thanks.
I'm afraid not. This is not something supported by the datastore. It can't do joins.
There are basically two ways around that limitation:
Denormalize and put an indexed copy of the 'name' data in your Foo object. You can do this somewhat automatically with an @OnSave
method. The downside is that you need to remember to update the data in both places if it changes.
Do the work of the query planner yourself. Query for the relevant AnObjects, then query for the relevant Foos.