title: JPA - @OneToMany as a Map
body: <p>This seems like a common enough case, but as JPA newbie, I am having trouble figuring this out. I'm using EclipseLink and PostgreSQL, but this should relate to just the JPA spec.</p>
  
  <p>I have one table <code>PRIMARY</code> that has an ID and then a bunch of other columns. There is another table <code>SECONDARY</code> that has a foreign key into the <code>PRIMARY</code> table also called <code>ID</code>. This <code>SECONDARY</code> table has a composite key of that <code>ID</code> and a varchar representing a locale.</p>
  
  <p>So, in the <code>Primary</code> entity I want to have a field of type <code>Map&lt;String, Secondary&gt;</code> where the key is the locale string from the <code>SECONDARY</code> table and the entry is the <code>Secondary</code> entity. My <code>Secondary</code> class looks like this:</p>
  
  <pre><code>@Entity
  public class Secondary
  {
       @Id
       private Long id;
       @Id
       private String locale;
       private String str1;
       private String str2;
       .....
  }
  </code></pre>
  
  <p>I'm thinking I want to use the <code>@MapKeyJoinColumn</code> annotation, but I can't seem to get the other annotations to work. I tried this:</p>
  
  <pre><code>@OneToMany
  @JoinColumn(name="ID")
  @MapKeyJoinColumn(name="LOCALE")
  private Map&lt;String, Secondary&gt; secondaryByLocale;
  </code></pre>
  
  <p>This resulted in it trying to select a column named secondaryByLocale_key which doesn't exist.</p>
  
  <p>I then tried this:</p>
  
  <pre><code>@OneToMany
  @JoinTable(name="SECONDARY", 
          joinColumns={@JoinColumn(name="ID")},
          inverseJoinColumns=@JoinColumn(name="ID"))
  @MapKeyJoinColumn(name="LOCALE")
  private Map&lt;String, Secondary&gt; secondaryByLocale;
  </code></pre>
  
  <p>This results in the following error:</p>
  
  <pre><code>Exception Description: The @JoinColumns on the annotated element [field secondaryByLocale] from the entity class [class com.foo.Primary] is incomplete. When the source entity class uses a composite primary key, a @JoinColumn must be specified for each join column using the @JoinColumns. Both the name and the referencedColumnName elements must be specified in each such @JoinColumn.
  </code></pre>
  
  <p>I tried adding the referencedColumnName to the annotation (I wasn't sure what it even should be), but I got the same error.</p>
  
  <p>As suggested, I tried using <code>@MapKey</code> as follows:</p>
  
  <pre><code>@OneToMany
  @JoinColumn(name="ID")
  @MapKey(name="LOCALE")
  private Map&lt;String, Secondary&gt; secondaryByLocale;
  </code></pre>
  
  <p>This results in the following error:</p>
  
  <pre><code>Exception Description: The map key [LOCALE] on the entity class [class com.foo.Secondary] could not be found for the mapping [org.eclipse.persistence.mappings.UnidirectionalOneToManyMapping[secondaryByLocale]].
  </code></pre>
  
  <p>Maybe I'm going about this all wrong, and there's a better way to annotate the Map field. Any help would be much appreciated.</p>