How do you specify OrderBy clause on two columns

Sam picture Sam · Oct 18, 2010 · Viewed 17.5k times · Source

We would like to orderBy 2 columns in the Seam EntityQuery interface as well as the JPA model. How do we do this?

@Entity
public class A{

@OrderBy(???) // should this be hardcoded here, is it database agnostic
List<B> bobjects;
}

@Entity
public class B {

   public short startTimeHrs;
   public short startTimeMins;
}

@Name("bList")
public class B extends EntityQuery {
        setOrderColumn("startTimeHrs, startTimeMins"); // Is this correct?
        setOrderDirection("asc");
}

Answer

Shervin Asgari picture Shervin Asgari · Oct 18, 2010

Yes you can use @OrderBy to order your queries.

@OrderBy("startTimeHrs, startTimeMins")
@OneToMany(...)
getBobjects() {
   return bobjects;
}

Now whenever you are saying A.getBobjects() they will be ordered. However, if you are using the EntityQuery way of retrieving the result you can override the getEjbql() and put the order by there.

@Name("bList")
public class B extends EntityQuery {
    @Override
    public String getEjbql() {
          return "select b from B b order by startTimeHrs, startTimeMins";
    }
}

Or you can @Override the getResultList() to manipulate the collection there in your entityLists