CrudRepositry 使用@Query 检查集合是否包含项目

CrudRepositry Using @Query to Check if Collection Contains an item

我不知道如何检查我的 User 是否在我的数据库中有某个 Role。具体来说,我想 运行 一个计数查询 - 并且想避免在数据库之外进行处理。

我们的代码库使用 org.springframework.data.repository.CrudRepository - 因此我们使用 @Query 来指定复杂的查询。 (org.springframework.data.jpa.repository.Query)

这 SQL 查询 return 我想要什么:

SELECT Count(*) 
FROM user 
where id in (
    select user_id 
    from user_role 
    where role_type = 'ROLE_USER'
);

但是我无法得到我想要的 @Query 到 return。

这是一些代码:

User class:

@Entity
@ApiModel(description = "Represents an user of the system")
public class User implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name = "ID")
    @ApiModelProperty(value = "The ID of the user", required = true)
    private Long id;

    @Column(name = "USERNAME", unique = true)
    @ApiModelProperty(value = "The userName of the user", required = true)
    private String username;

    @Column(name = "STATUS", nullable=false)
    @Enumerated(EnumType.STRING)
    @ApiModelProperty(value = "The status of the user", required = true)
    private UserStatus status;

    @Column(name = "PASSWORD")
    @ApiModelProperty(value = "The encrypted password of the user")
    private String password;

    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
    @JoinColumn(name = "USER_ID", nullable=false)
    @ApiModelProperty(value = "The role of the user", required = true)
    private Set<UserRole> userRoles;
}

UserRole Class:

@Entity
public class UserRole implements Serializable {

    @Id
    @Column(name = "ID")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(name = "ROLE_TYPE")
    @Enumerated(EnumType.STRING)
    private UserRoleType roleType;

    @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name = "USER_ID", nullable=false, insertable=false, updatable=false)
    @ApiModelProperty(value = "The id the user (link to User table)", required = true)
    private User user;
}

存储库 Class:

//这个位不起作用-但在这里显示我想做的事情的意图!

public interface UserDao extends CrudRepository<User, Long> {    

    @Query("select count(u) from User u where u.status='ACTIVE' and u.userRoles contains 'ROLE_USER')
    public long countAllActiveUser();


}

数据库非常简单,包含 User table 和 User_Role table。

User table 有 IdUsernamePasswordStatus 列。

例如

User_Role table 有 IdRole_TypeUser_Id 列。

例如

在上面的例子中 - 计数 SQL 的答案是 2! (有 2 个用户是 ACTIVE 并且具有角色:ROLE_USER

您可以在存储库界面中使用一个简单的函数来解决这个问题:

long countByStatusAndUserRoles_roleType(status: UserStatus, roleType: UserRoleType)

这个查询应该做同样的事情:

@Query("select count(u) from User u left join u.userRoles ur where u.status = :status and ur.roleType = :roleType")
long countAllActiveUser(status: UserStatus, roleType: UserRoleType);

您当然可以硬编码 statusroleType

所以你首先需要加入实体(表)。我选择了左联接,因为您需要来自两个实体的项目,请参阅 here 以获取有关联接类型的更详细答案。如果通过上下文给定,JPA 连接将自动连接正确的键,否则会出现错误。

然后你可以像往常一样指定条件。