title: How to get a HashMap result from Spring Data Repository using JPQL?
body: <p>I've currently implemented a Ternary relationship using Map between <strong>User</strong>, <strong>Shop</strong> and <strong>Role</strong>.
  So in my User Entity i have this mapping:</p>
  <p><strong>User.java</strong></p>
  <pre><code>@Entity
  @Table(name = &quot;us_users&quot;)
  public class User implements Serializable {
  
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(nullable = false)
  private int id;
  
  @Basic(fetch = FetchType.LAZY, optional = false)
  private String uuid;
  
  @NotBlank(message = &quot;First name can not be empty&quot;)
  @Column(name = &quot;usFname&quot;)
  private String usFname;
  
  @NotBlank(message = &quot;Username can not be left empty&quot;)
  @Column(name = &quot;us_lname&quot;)
  private String usLname;
  
  @NotBlank(message = &quot;Email field can not be empty&quot;)
  @ValidEmail
      @Column(name = &quot;usEmail&quot;, unique = true)
      @Basic(fetch = FetchType.LAZY, optional = false)
      private String usEmail;
   //this is the actual mapping
  //a user can have a list of shops, but one role assigned for each shop
  @ManyToMany
      @MapKeyJoinColumn(name = &quot;shop_fk&quot;)
      @JoinTable(name = &quot;user_shop_role&quot;,
              joinColumns = @JoinColumn(name = &quot;user_fk&quot;), inverseJoinColumns = @JoinColumn(name = &quot;role_fk&quot;))
      private Map&lt;Shop, Role&gt; shopRoleMap = new HashMap&lt;&gt;();
  //GETTERS AND SETTERS,
  }
  </code></pre>
  <p><strong>Role.java</strong></p>
  <pre><code>@Entity
  @Table(name = &quot;ro_roles&quot;)
  public class Role {
  
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(nullable = false)
  private int id;
  
  @Column(name = &quot;rn_role_name&quot;)
  private String roleName;
  
  public int getId() {
      return id;
  }
  
  public void setId(int id) {
      this.id = id;
  }
  
  public String getRoleName() {
      return roleName;
  }
  
  public void setRoleName(String roleName) {
      this.roleName = roleName;
  }
  </code></pre>
  <p>}</p>
  <p><strong>Shop.java</strong></p>
  <pre><code>@Entity
  @Table(name = &quot;sh_shops&quot;)
  public class Shop {
  @Id
  @GeneratedValue(strategy=GenerationType.IDENTITY)
  @Column(nullable = false)
  private int id;
  
  @NotBlank(message = &quot;Shop name can not be left blank&quot;)
  private String sh_name;
  
  @NotBlank(message = &quot;please provide shop icon&quot;)
  private String sh_icon;
  
  @NotBlank(message = &quot;please fill the shop description&quot;)
  private String sh_description;
  
  @NotBlank
  private String sh_tag;
  //setters, getters, equals, hashCode methods.
  }
  </code></pre>
  <p>This mapping gives me another table (<strong>user_shop_role</strong>) in the database that has <strong>user_fk</strong>,<strong>shop_fk</strong> and <strong>role_fk</strong> which is lovely.
  The difficult bit is to make a Query using Spring Data JPA and get the result as a HashMap for a given user as this: HashMap&lt;Shop,Role&gt; for the logged in user.</p>
  <p>At the UserRepository layer i have tried this:</p>
  <p>interface <strong>UserRepository</strong> extends JpaRepository and i have this query...</p>
  <pre><code>@Query(&quot;SELECT u FROM User u join shopRoleMap m where key(m)= usEmail&quot;)
       public HashMap&lt;Shop, Role&gt; findByUser(String email);
  
  
  }
  </code></pre>
  <p>How can i get results for a ternary relationship mapped using a Map Collection to get a list of shops and corresponding role value. More of a key,value result ?</p>