title: I have to persist a Map<EntityType, List<EntityType>> with JPA
body: <p>I'm trying to persist an Entity that has a Map as one of its values. To be more precise. I have the @Entity Request that have a compound primary key with three elements. This primary key is composed by an id, the User an <code>Map&lt;EntityType, List&lt;EntityType&gt;&gt;</code> where the first EntityType is the selected service and the related value is the list of the items where the service will be applied to.
  Below the code that I have but I'm missing the annotation that i have to use for the Map. I read online that the good way to go is the create a wrapper entity like to one that i created (SelectedService2MyItem) that holds the list and then the map is just a key-value pair between two entity but I can't make it works and I don't know how to proceed. 
  Does anyone can help me?</p>
  
  <p><strong>Request Entity</strong></p>
  
  <pre><code>@Entity
  public class Request {
  
      @EmbeddedId
      private RequestId id;
  
      @Column
      private String name;
  
      @ManyToOne
      @JoinColumn(name = "user_id", foreignKey = @ForeignKey(name = "FK_user_id"), nullable=false)
      private User user;
  //Getter, setter, constructor omitted
  }
  </code></pre>
  
  <p><strong>RequestId</strong></p>
  
  <pre><code>@Embeddable
  public class RequestId {
  
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long id;
  
  @ManyToOne
  @JoinColumn(name = "user_id", foreignKey = @ForeignKey(name = "FK_user_id"), nullable=false)
  private User user;
  
  private Map&lt;ServiceOffered, SelectedService2MyItem&gt; service2MyItem = new HashMap&lt;ServiceOffered, SelectedService2MyItem&gt;();
  
  //Getter, setter, constructor omitted
  }
  </code></pre>
  
  <p><strong>SelectedService2MyItem</strong></p>
  
  <pre><code>@Entity
  public class SelectedService2MyItem {
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      @Column(name = "id", updatable = false, nullable = false)
      private Long id;
  
      @OneToMany(mappedBy = "id")
      private List&lt;MyItem&gt; myItemsSelected;
  
  //Getter, setter, constructor omitted
  }
  </code></pre>