title: JpaRepository find User with Role in list of roles
body: <p>I'm using <code>Spring</code> with <code>Hibernate</code> and <code>JpaRepository</code> as database repository.</p>
  
  <p>I have two classes for user storage:</p>
  
  <pre><code>@Entity
  public class User {
      @Id
      private Long id;
  
      private String username;
  
      private String password;
  
      @OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
      private List&lt;UserRole&gt; roles;
  }
  
  @Entity
  public class UserRole {
      @Id
      private Long id;
  
      @ManyToOne
      @JoinColumn(name = "user_id")
      private User user;
  
      @Enumerated(EnumType.STRING)
      private Role role;
  }
  
  public enum Role {
      ADMIN,
      MEMBER;
      /* some others in the future */
  }
  </code></pre>
  
  <p>As you can see <code>User</code> can have multiple roles assigned. So <code>user1</code> can have <code>ADMIN</code> and <code>MEMBER</code> roles and <code>user2</code> only <code>MEMBER</code> role.</p>
  
  <p>I would like to user with <code>ADMIN</code> role (among others) could list all users in database (<code>JpaRepository</code> <code>findAll()</code> method is enough) but user with only <code>MEMBER</code> role could list only users with <code>MEMBER</code> role.</p>
  
  <p>How to write method in <code>JpaRepository</code> to achieve that? I tried some below but it's not working:</p>
  
  <pre><code>List&lt;User&gt; findByRoles_RoleIn(Collection&lt;Role&gt; roles);
  </code></pre>
  
  <p>or</p>
  
  <pre><code>List&lt;User&gt; findByRoles_Role(Role role);
  </code></pre>
  
  <p>Maybe some custom <code>@Query</code>?</p>