How can I at runtime specify a sort order for an associated Set of entities?
Given the following Company class and a query like " from Company order by foundedDate ", I'd like to be able to sometimes iterate over employees in name order and other times in age order.
class Company { Set employees = new HashSet(); }
I could use the @Sort annotation as @Sort(type = SortType.COMPARATOR, comparator = SomeComparator.class), but that will only let me have one order. As far as I know there's no way at runtime to specify a different comparator.
I could also use Collection orderedCollection = session.filter( collection, "order by this.amount" ), but that doesn't change the iteration order of company.getEmployees(). If I have a list of companies, I have to call session.filter for each company. I'd like a way at runtime to specify how I want employees ordered.
I tried @Filter(name = "someFilter", condition = " order by name asc ") and while you can put more than one @Filter on an entity association, you can't use "order by" in the condition (kind of strange because you can use "order by" with session.filter()).
So I am stuck. It seems like a simple thing and like it would be easy for Hibernate to implement. If anyone knows how to do this, I would be extatic.
|