Search code examples
google-app-enginegoogle-cloud-endpointsobjectify

Google app engine, objectify how to order by a sub entity field?


I have a Course entity that contains the following field

@Index
private @Load
Ref<Student> student;

The student entity then has the field

 @Index
private String matric;

I want to load all the Course entities sorted using the students matric number.

I have tried using the "." operator to get the sub field like this

ofy().load().type(Course.class).filter("course", course).order("student.matric").list();

but this return no result.

Is it possible to do this? how?


Solution

  • I don't think that is possible with objectify. I would let Course implement Comparable:

    @Entity
    public class Course implements Comparable<Course> {
        .
        .
        .
        @Override
        public int compareTo(Course otherCourse) {
            return this.getStudent().getMatric().compareTo(otherCourse.getStudent().getMatric());
        }
    }
    

    Remove the "order" part of the Objectify load and use Collections.sort() instead:

    List<Course> courses = ofy().load().type(Course.class).filter("course", course).list();
    Collections.sort(courses);