如何创建 spring 数据查询,仅 returns 那些具有包含传递的用户对象的 UserRole 的实体?

How to create a spring data query that returns only those entities that have a UserRole which contains the passed user object?

我正在尝试编写一个 spring 数据 jpa 查询,该查询为我提供所有具有包含传递的(或当前 spring 安全主体)用户的 UserRole 的仪表板实体。

相关代码:

@Entity
public class Dashboard extends BaseModel {

    private String name;

    @ManyToMany
    private Set<UserRole> allowedUserRoles;     
}

@Entity    
public class UserRole {
    @Id
    @NotNull
    private String uid;

    @ManyToMany(mappedBy = "userRoles")
    private Set<User> users = new HashSet();
}

@Entity
public class User implements UserDetails {

    @Id
    @NotNull
    private String username;

    private String uuid;

    private String displayName;

    private String email;

    private boolean active = false;
    private boolean superadmin = false;

    @ManyToMany(fetch = FetchType.EAGER)
    private Set<UserRole> userRoles = new HashSet<>();
}

@RepositoryRestResource(collectionResourceRel = "dashboards", path = "dashboards")
public interface DashboardRepository extends JpaRepository<Dashboard, Long> {

    @Query("...")
    List<Dashboard> findWithPermission(User user);
}

最好的, 蒂姆

传递的用户对象必须已经有一个 ID,大概是您在查找用户名和密码时找到的,只需使用它来查找分配给用户的权限

最后的交叉 table 应该是

useridsxroleidxpermission1id useridsxroleidxpermission2id 等...

尝试使用 MEMBER OF

@Query("SELECT DISTINCT d FROM Dashboard d WHERE :user MEMBER OF d.allowedUsers.users")