Hi Shon. I'm afraid I'm not going to answer your question directly, because I've got no CLUE how to set up that complex Order By in HQL or in a Criteria query. ;) However, maybe a different perspective on things will help you work it out.
First of all, let me restate what I think you're trying to do. You want a list of Projects, all of which are in the "WAITING FOR DESIGN" status, and you want them ordered by their creation date (which is determined by a status record), and then their ID number.
If I understood that correctly, here's how I'd go about it.
First of all, just query a list of Projects in the right state.
HQL:
Code:
FROM Project as p
WHERE p.currentStatus.name = 'WAITING FOR DESIGN'
I don't use criteria queries often, but that should look something like this:
Code:
List projects = session.createCriteria(Project.class, "proj")
.createAlias("currentStatus", "stat")
.add( Restriction.eq("stat.name", "WAITING FOR DESIGN") )
.list();
Now, you've got a list of Project objects, and you need them ordered before you do anything to them, right? First of all, create a helper method on Project called getCreateDate that will find your creation date using the list of Status objects in your statusHistory collection. Once you've got that, all you need is a comparator that will do your order-by:
Code:
class ProjectOrderByCreateAndId
implements Comparator
{
public boolean equals(Object o1, Object o2)
{
return o1.equals(o2);
}
public int compare(Object o1, Object o2)
{
// ... Code here to check for the right classes
Project p1 = (Project)o1;
Project p2 = (Project)o2;
int createDateComp = p1.getCreationDate().compareTo(p2.getCreationDate());
if (createDateComp == 0)
{
return p1.getId() - p2.getId();
}
else
{
return createDateComp;
}
}
}
Then, when you need the result list sorted:
Code:
List sortedProjects = Collections.sort(projectList, new ProjectOrderByCreateAndId());
Is that the best way to do it? I've got no clue. But, since I don't know how to set up the complex HQL that you'd need to have Hibernate determine the creation date so the database can order it, this way would work for me. :)
-- Kin