Hibernate Order By Association
I'm using Hibernate 3.2, and using criteria to build a query. I'd like to add and 'order by' for a many-to-one association, but I don't see how that can be done. The Hibernate que
Solution 1:
Ok, found the answer. I tried something that I didn't think would work, but to my surprise did. I was trying this:
Criteriacriteria=super.getSession().createCriteria(WipDiscreteJob.class);
criteria.addOrder(Order.asc("assnName.propertyName"))
but what actually worked was:
Criteriacriteria=super.getSession().createCriteria(WipDiscreteJob.class);
CriteriaassnCrit= criteria.createCriteria("assnName");
assnCrit.addOrder(Order.asc("propertyName"));
I had made the assumption that the addOrder() method was only usable on the main criteria and not on any association criteria.
Solution 2:
I was having the same issue and it can also be solved like this:
Criteriacriteria=super.getSession().createCriteria(WipDiscreteJob.class)
.createAlias("assnName","a")
.addOrder(Order.asc("a.propertyName"));
createAlias lets you keep your criteria rooted on your original entity (WipDiscreteJob.class in this case) so that you can keep building your criteria up in case you needed it (for example, if you need a second order by property from you original entity).
Post a Comment for "Hibernate Order By Association"