hibernate order by association

前端 未结 2 832
隐瞒了意图╮
隐瞒了意图╮ 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:36

    I was having the same issue and it can also be solved like this:

    Criteria criteria = 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).

提交回复
热议问题