title: JPQL query to fetch all roles of user based on user id
body: <p>In my project I have many-to-many relationship between User and Role. For this reason I have also new entity <code>UserRole</code> which connects this two entities.</p>
  <p>It looks like this:</p>
  <p>User:</p>
  <pre><code>@Data
  @Entity
  @Table(NAME = &quot;USERS&quot;)
  public class User {
      @Id
      @Column(name = &quot;USER_ID&quot;)
      private String userId;
      @Basic
      @Column(name = &quot;EMAIL&quot;)
      private String email;
     
      @OneToMany(fetch = LAZY, mappedBy = &quot;user&quot;)
      private Set&lt;UserRole&gt; userRoles;
  }
  </code></pre>
  <p>Role:</p>
  <pre><code>@Data
  @Entity
  @Table(NAME = &quot;ROLES&quot;)
  public class Role {
      @Id
      @Column(name = &quot;ROLE_ID&quot;)
      private String roleId;
      @Basic
      @Column(name = &quot;NAME&quot;)
      private String name;
     
      @OneToMany(fetch = LAZY, mappedBy = &quot;role&quot;)
      private Set&lt;UserRole&gt; userRoles;
  }
  </code></pre>
  <p>UserRole:</p>
  <pre><code>@Data
  @Entity
  @IdClass(UserRolePK.class)
  @Table(NAME = &quot;USER_ROLES&quot;)
  public class UserRole {
      @Id
      @Column(name = &quot;USER_ID&quot;)
      private String userId; 
  
      @Id
      @Column(name = &quot;ROLE_ID&quot;)
      private String roleId;
      
      @ManyToOne(fetch = LAZY)
      @JoinColumn(name = &quot;USER_ID&quot;, insertable = false, updatable = false)
      private User user;
  
      @ManyToOne(fetch = LAZY)
      @JoinColumn(name = &quot;ROLE_ID&quot;, insertable = false, updatable = false)
      private Role role;
  }
  </code></pre>
  <p><strong>In this scenario User can have multiple roles.</strong></p>
  <p><strong>Question:</strong> How to fetch User by his id (userId) with all assigned to him Roles using one query (JPQL)?</p>
  <p>I know I can first fetch User by id, and than I can fetched separately Roles based on UserRole table.</p>
  <p>But I want to do that in one query. I want to have User with List of Roles.</p>