休眠动态顺序 [英] Hibernate Dynamic Order

查看:78
本文介绍了休眠动态顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想按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?

推荐答案

我认为不可能将命名参数放在where子句之外.

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

有关HQL case的更多信息

More on case in HQL docs :

对于您的特定问题(在其他用户之前先有管理员),我建议进行两个查询并在Java中合并两个列表.

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

这篇关于休眠动态顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆