Hibernate Dynamic Order

纵饮孤独 提交于 2019-12-02 20:17:39

问题


Hi i want to sort in HQL

ORDER BY IF g.groupAdminId=:adminid THEN 1 ELSE 0 END ASC

But it doesn't work, i want to have all entities where the user is admin first, how can i archieve this?


回答1:


I don't believe it is possible to put named parameters outside a where clause.

It is possible to order according to expressions:

from User U
order by case
  when U.group.name = 'Admin' then 0
  when U.group.name = 'Superuser' then 1
  else 2
end asc

More on case in HQL docs :

For your particular problem (having admins before other users) I suggest making two queries and combining the two lists in Java.

There are other ways around this but I do not like any of them:

  • Multiple mappings
  • Custom functions


来源:https://stackoverflow.com/questions/9262413/hibernate-dynamic-order

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!