Hibernate Dynamic Order

后端 未结 1 1749
挽巷
挽巷 2021-01-28 21:43

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

相关标签:
1条回答
  • 2021-01-28 22:15

    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
    0 讨论(0)
提交回复
热议问题