[英]Jpa repository find all with type HashSet
In my Spring Boot application, I have two entities user and authorities. 在我的Spring Boot应用程序中,我有两个实体用户和权限。 There are three parameters ROLE_USER, ROLE_ADMIN, and ROLE_SYSTEM is assignable to the user as Authority and one user can have multiple authorities.
可将三个参数ROLE_USER,ROLE_ADMIN和ROLE_SYSTEM分配给用户作为“权限”,并且一个用户可以具有多个权限。 in the user entity I have created join column which joins user and authority, so I can assign multiple authority to a user using Join table, the Entity field is shown below:
在用户实体中,我创建了连接用户和权限的连接列,因此我可以使用连接表为用户分配多个权限,实体字段如下所示:
@JsonIgnore
@ManyToMany
@JoinTable(
name = "jhi_user_authority",
joinColumns = {@JoinColumn(name = "user_id", referencedColumnName = "id")},
inverseJoinColumns = {@JoinColumn(name = "authority_name", referencedColumnName = "name")})
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@BatchSize(size = 20)
private Set<Authority> authorities = new HashSet<>();
so, using this field hibernate created the third table which has a user id and authority role. 因此,hibernate使用此字段创建了第三个具有用户ID和授权角色的表。 In Repository, I have used Hibernate JPARepository.
在存储库中,我使用了Hibernate JPARepository。 and using findAll() I am getting all users.
并使用findAll()获得所有用户。 Controller:
控制器:
In Controller: 在控制器中:
@GetMapping("/users")
@Timed
@Secured({ AuthoritiesConstants.ADMIN, AuthoritiesConstants.SYSTEM })
public ResponseEntity<List<UserDTO>> getAllUsers(Pageable pageable) {
final Page<UserDTO> page = userService.getAllManagedUsers(pageable);
HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/users");
return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
}
In Service: 服务中:
@Transactional(readOnly = true)
public Page<UserDTO> getAllManagedUsers(Pageable pageable) {
return userRepository.findAll(pageable).map(UserDTO::new);
}
In Repository: 在存储库中:
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
Page<User> findAll(Pageable pageable);
}
I have tried with this in the repository: 我已经在存储库中尝试过此操作:
@Query("select users from User users where users.authorities = 'ROLE_USER'")
Page<User> getAllByAuthoriy(Pageable pageable);
Authority entity: 授权实体:
@Entity
@Table(name = "jhi_authority")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Authority implements Serializable {
private static final long serialVersionUID = 1L;
@NotNull
@Size(max = 50)
@Id
@Column(length = 50)
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
So, Now I have to change the query from findAll to findAllByAuthorities(Pageable pageable, set authorities). 因此,现在我必须将查询从findAll更改为findAllByAuthorities(Pageable pageable,设置权限)。 but this not works, showing type casting error.
但这不起作用,显示类型转换错误。 How to fetch all that user which role is ROLE_USER?
如何获取所有该角色为ROLE_USER的用户?
Here is the solution, I have to write a query for getting users from user table which authority is a user_role. 这是解决方案,我必须编写一个查询以从权限为user_role的用户表中获取用户。 for that, I have to pass a string to match in HashSet.
为此,我必须传递一个字符串以匹配HashSet。
@Query("select users from User users where 'ROLE_USER' member of users.authorities")
Page<User> getAllByAuthoriy(Pageable pageable);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.