【问题标题】:Find user with role (list of roles) who has exactly these roles from list and no other查找具有角色(角色列表)的用户,该用户从列表中完全具有这些角色,而没有其他角色
【发布时间】:2020-06-02 19:18:42
【问题描述】:

问题描述;

我有一组角色的用户模型(多对多关系)。 我想从具有我所要求的角色的数据库(MySQL)用户中挑选(不多不少)。

如果我正在寻找具有管理员和用户角色的用户,这是我的查询(无法正常工作):

select * from login.user u join user_role ur on u.id = ur.user_id join role r on ur.role_id = r.id where r.name in ('ROLE_ADMIN', 'ROLE_USER')

等效于 Hibernate (NativeQuerty);

select * from user u join user_role ur on u.id = ur.user_id join role r on ur.role_id = r.id where r.name in (:roles)

此查询返回具有管理员用户角色的用户。 在这种情况下,我关注的是管理员用户。

如果询问具有管理员角色的用户,我希望拥有只有管理员角色的用户。

【问题讨论】:

    标签: spring hibernate jpa nativequery


    【解决方案1】:

    好吧,您基本上是在查询中说“给我任何具有 'ROLE_ADMIN' 或 'ROLE_USER' 或两者兼有的用户”。如果您只指定您感兴趣的角色,您将获得必要的结果。

    这仅返回管理员,例如:

    select * from login.user u join user_role ur on u.id = ur.user_id join role r on ur.role_id = r.id where r.name in ('ROLE_ADMIN')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-12-12
      • 2014-07-16
      • 2017-09-10
      • 2017-11-02
      • 2017-02-06
      • 2013-04-06
      • 2011-05-22
      相关资源
      最近更新 更多