JPA实现对Set集合的查询

User.java

/**
 * A user.
 */
@Entity
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class User  implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;


    @NotNull
    @Size(min = 1, max = 50)
    @Column(length = 50, unique = true, nullable = false)
    private String login;


    @NotNull
    @Size(min = 5, max = 100)
    @Column(length = 100)
    @JsonSerialize(using = CustomPasswordSerializer.class)
    private String password;


    @Size(max = 50)
    @Column(name = "first_name", length = 50)
    private String firstName;


    @Size(max = 50)
    @Column(name = "last_name", length = 50)
    private String lastName;
  
    @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)
    private Set authorities = new HashSet<>();


    public Long getId() {
        return id;
    }


    public void setId(Long id) {
        this.id = id;
    }


    public String getLogin() {
        return login;
    }


    public void setLogin(String login) {
        this.login = login;
    }


    public String getPassword() {
        return password;
    }


    public void setPassword(String password) {
        this.password = password;
    }


    public String getFirstName() {
        return firstName;
    }


    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }


    public String getLastName() {
        return lastName;
    }


    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
  
    public Set getAuthorities() {
        return authorities;
    }


    public void setAuthorities(Set authorities) {
        this.authorities = authorities;
    }

Authority.java

/**
 * An authority (a security role) used by Spring Security.
 */
@Entity
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Authority implements Serializable {


    @NotNull
    @Size(min = 0, max = 50)
    @Id
    @Column(length = 50)
    private String name;


    public Authority(String name) {
        this.name = name;
    }


    public Authority() {


    }


    public String getName() {
        return name;
    }


    public void setName(String name) {
        this.name = name;
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }


        Authority authority = (Authority) o;


        if (name != null ? !name.equals(authority.name) : authority.name != null) {
            return false;
        }


        return true;
    }


    @Override
    public int hashCode() {
        return name != null ? name.hashCode() : 0;
    }


    @Override
    public String toString() {
        return "Authority{" +
            "name='" + name + '\'' +
            "}";
    }
}


action类

/**
 * REST controller for managing Order.
 */
@RestController
@RequestMapping("/api")
public class OrderResource {

    @RequestMapping(value = "/order/REPlist",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @Timed
    public ResponseEntity> getREPlist() throws URISyntaxException{
    log.debug( "REP list" );
    List uList = this.userRepository.findAll(new Specification() {
@Override
public Predicate toPredicate(Root root,CriteriaQuery query, CriteriaBuilder cb) {
Predicate predicate = cb.conjunction();
List> expressions = predicate.getExpressions();

SetJoin authoritySetJoin = root.join(root.getModel().getSet("authorities", Authority.class), JoinType.LEFT);

expressions.add( cb.or( cb.equal(authoritySetJoin.get("name"), AuthoritiesConstants.ADMIN ) ,
cb.equal(authoritySetJoin.get("name"), AuthoritiesConstants.FINANCE ) ,
cb.equal(authoritySetJoin.get("name"), AuthoritiesConstants.SALES ) ,
cb.equal(authoritySetJoin.get("name"), AuthoritiesConstants.DATAM ) ,
cb.equal(authoritySetJoin.get("name"), AuthoritiesConstants.CS )   ) );
                                // 这里 AuthoritiesConstants 是各种权限(String 常量)
return predicate;
}
});
    HttpHeaders headers = PaginationUtil.generatePaginationHttpHeadersForFrontWeb(new PageInfo());
        return new ResponseEntity>(uList,headers,HttpStatus.OK);
    }
}

你可能感兴趣的:(JPA)