简体   繁体   English

我想通过给出特定的角色顺序来使用 orderBy

[英]I want to user orderBy by giving specific order of role

I've roles like lead > collaborator > participant > viewer.我有领导 > 合作者 > 参与者 > 查看者等角色。 If I use orderBy(role) then getting order as collaborator > lead > participant > viewer If I do DESC order then getting reverse.如果我使用 orderBy(role) 然后以协作者 > 领导 > 参与者 > 查看者的身份获得订单如果我使用 DESC 订单然后获得反向。

I tried specifying order in SQL and it's working as expected getting lead > collaborator > participant > viewer我尝试在 SQL 中指定顺序,它按预期工作得到领导 > 合作者 > 参与者 > 查看者

ORDER BY ROLE = 'viewer', ROLE = 'participant', ROLE = 'collaborator', ROLE = 'lead';

The same I want to achieve in Java Spring Boot.同样我想在 Java Spring Boot 中实现。 How can I?我怎么能够?

You have to create this method in your service:您必须在您的服务中创建此方法:

public List<ConcoursAdministratifRe> getListByRole() {
String queryRecherche = "SELECT t FROM YourEntityName t ORDER BY (CASE role WHEN 'viewer' THEN 1 WHEN 'participant' THEN 2 WHEN 'lead' THEN 3 WHEN 'collaborator' THEN 4 ELSE 5 END) ASC";
TypedQuery<YourEntityName > query = em.createQuery(queryRecherche, YourEntityName.class);
List<YourEntityName> myList = query.getResultList();
return myList;

If your roles are static (seldom if ever changes) then you can accomplish this by creating a enum ( Create Emun and Alter Type enum ) for them.如果您的角色是 static(很少更改),那么您可以通过为它们创建一个enumCreate EmunAlter Type enum )来完成此操作。 (see demo ) (见演示

create type role_enum as enum ('collaborator', 'lead', 'participant', 'viewer');
 
create table test ( id   integer  generated always as identity
                                  primary key 
                  , name text
                  , role role_enum
                  ) ; 

I am not sure how to accomplish this in your obscurification manager (Spring Boot) so you will have to translate it or use raw sql.我不确定如何在您的模糊化管理器 (Spring Boot) 中完成此操作,因此您必须翻译它或使用原始 sql。

I've achieved this by specifying the order我通过指定顺序实现了这一点

.orderBy(field("role").sortAsc("lead", "collaborator", "participant", "viewer"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我希望用户通过我的登录要求才能登录 - I want the user to pass my login requirement in order to sign in 如何在 spring 引导中验证具有特定角色的用户? - How to authenticate user with specific role in spring boot? JpaRepository查找或创建用户角色返回null并给出错误 - JpaRepository find or create user role return null and giving error OrderBy 查询 - 我可以按存储在 firebase 中的孙子“投票”进行订购吗? - OrderBy query - Can I order by the grandchild "Vote" stored in firebase? 我想要特定的模式 - I want specific pattern 如果我想使用我知道在大多数用户的计算机上不可用的特定字体 - If I want to use a specific font that I know it is not available in most of the user's computer Spring Switching User:仅允许具有特定角色的用户切换到具有特定角色的另一个用户 - Spring Switching User: only allow a user with a specific role switch to another user with specific roles Liferay kaleo工作流程通知,将电子邮件发送给特定用户而不是角色 - Liferay kaleo workflow notifications, send email to specific user not to role 如何根据用户角色访问特定的数据库记录? - How to access specific db records based on user role? Spring 无特定角色用户的安全测试端点 - Spring Security testing Endpoint for user without specific role
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM