【发布时间】:2019-01-28 04:39:36
【问题描述】:
我不知道如何检查我的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 表有 Id、Username、Password、Status 列。
例如
- 1,戴夫,****,活跃
- 2,约翰,****,活跃
User_Role 表有 Id、Role_Type、User_Id 列。
例如
- 1,ROLE_USER,1
- 2, ROLE_ADMIN, 1
- 3,ROLE_USER,2
在上面的例子中 - 计数 SQL 的答案是 2! (有2个用户是ACTIVE,角色为:ROLE_USER)
【问题讨论】:
-
所以您的
User_Role表中有重复条目? -
您是否考虑过将用户角色设为枚举?
-
@chrylis :没有重复 - 只是多个角色! (我纠正了错误!)
-
@PinkieSwirl - 用户角色类型是枚举。这个结构在我来之前就已经实现了!
-
好的,现在您编辑了您的问题以提供不同的数据,正确答案是 2。(如果您想说“2 是正确答案,但这不是我得到的",那么您需要更具体地了解正在发生的事情。)
标签: java spring spring-data-jpa repository jpql