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

时间:2018-08-21 23:34:12

标签: java spring spring-data-jpa repository jpql

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

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

此SQL查询返回我想要的内容:

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

但是我无法获得@Query来返回我想要的东西。

以下是一些代码:

User类:

@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类:

@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;
}

以及存储库类:

//此位不起作用-但这是为了显示我想做的事的意图!

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表和User_Role表的数据库非常简单。

User表具有IdUsernamePasswordStatus列。

例如

  • 1,戴夫,****,活跃
  • 2,约翰,****,活跃

User_Role表具有IdRole_TypeUser_Id列。

例如

  • 1,ROLE_USER,1
  • 2,ROLE_ADMIN,1
  • 3,ROLE_USER,2

在上面的示例中-count SQL的答案为2! (有2个用户,ACTIVE,其角色为:ROLE_USER

1 个答案:

答案 0 :(得分:0)

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

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连接,它将自动使用正确的键进行连接,否则会出错。

然后您可以照常指定条件。