由协会为了冬眠 [英] hibernate order by association
问题描述
我使用Hibernate的3.2,并使用标准来构建查询。我想借来添加和使用many-to-one关联,但我没有看到如何可以做到的。
Hibernate查询最终会这样看,我猜:
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 query would end up looking like this, I guess:
select t1.a, t1.b, t1.c, t2.dd, t2.ee
from t1
inner join t2 on t1.a = t2.aa
order by t2.dd <-- need to add this
我试过criteria.addOrder(assnName.propertyName),但它不工作。我知道它可以用于正常的属性来完成。我缺少的东西吗?
I've tried criteria.addOrder("assnName.propertyName") but it doesn't work. I know it can be done for normal properties. Am I missing something?
推荐答案
好吧,找到了答案。我试过的东西,我没有想到会工作,但让我吃惊的那样。我试图这样的:
Ok, found the answer. I tried something that I didn't think would work, but to my surprise did. I was trying this:
Criteria criteria = super.getSession().createCriteria(WipDiscreteJob.class);
criteria.addOrder(Order.asc("assnName.propertyName"))
但什么实际工作是:
but what actually worked was:
Criteria criteria = super.getSession().createCriteria(WipDiscreteJob.class);
Criteria assnCrit = criteria.createCriteria("assnName");
assnCrit.addOrder(Order.asc("propertyName"));
我已作出这样的假设addOrder()方法是仅在主要标准,而不是对任何关联的标准使用。
I had made the assumption that the addOrder() method was only usable on the main criteria and not on any association criteria.
这篇关于由协会为了冬眠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!