hibernate order by association

前端 未结 2 830
隐瞒了意图╮
隐瞒了意图╮ 2021-02-07 15:13

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 Hiber

2条回答
  •  心在旅途
    2021-02-07 15:45

    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"));
    

    I had made the assumption that the addOrder() method was only usable on the main criteria and not on any association criteria.

提交回复
热议问题