title: ManyToMany relationship using JPA with Hibernate provider is not creating primary keys
body: <p>I've created two JPA entities (Client, InstrumentTraded) using Hibernate as a provider that have a ManyToMany relationship.  After letting Hibernate generate the tables for MySQL it appears that the ManyToMany relationship table does not contain primary keys for the two foreign keys.  This allows duplicate records in the many-to-many table, which is not the desired result.</p>
  
  <p><strong>Tables generated:</strong>  </p>
  
  <pre><code>client(id,name)  
  instrument_traded(id,name)  
  client_instrument_traded(FK client_id, FK instrument_traded_id)
  </code></pre>
  
  <p><strong>Preferred table:</strong>  </p>
  
  <pre><code>client_instrument_traded(PK,FK client_id, PK,FK instrument_traded_id)
  </code></pre>
  
  <p><strong>Entities:</strong></p>
  
  <pre><code>@Entity
  public class Client extends AbstractEntity&lt;Integer&gt; {
  
      private static final long serialVersionUID = 1L;
  
      @Basic(optional = false)
      @Column(nullable = false, length = 125)
      private String name;
  
      @ManyToMany(fetch = FetchType.LAZY)
      @JoinTable(joinColumns = {
          @JoinColumn}, inverseJoinColumns = {
          @JoinColumn(name = "instrument_traded_id")}, uniqueConstraints =
      @UniqueConstraint(name = "UK_client_instruments_traded_client_id_instrument_traded_id",
      columnNames = {"client_id", "instrument_traded_id"}))
      @ForeignKey(name = "FK_client_instruments_traded_client_id",
      inverseName = "FK_client_instruments_traded_instrument_traded_id")
      private List&lt;InstrumentTraded&gt; instrumentsTraded;
  
      public Client() {
      }
  
      public List&lt;InstrumentTraded&gt; getInstrumentsTraded() {
          return instrumentsTraded;
      }
  
      public void setInstrumentsTraded(List&lt;InstrumentTraded&gt; instrumentsTraded) {
          this.instrumentsTraded = instrumentsTraded;
      }
  
      ...
  }
  
  
  
  @Entity
  @Table(uniqueConstraints = {
      @UniqueConstraint(name = "UK_instrument_traded_name", columnNames = {"name"})})
  public class InstrumentTraded extends AbstractEntity&lt;Integer&gt; {
  
      private static final long serialVersionUID = 1L;
  
      @Basic(optional = false)
      @Column(nullable = false, length = 50)
      private String name;
  
      @ManyToMany(mappedBy = "instrumentsTraded", fetch = FetchType.LAZY)
      private List&lt;Client&gt; clients;
  
      public InstrumentTraded() {
      }
  
      public List&lt;Client&gt; getClients() {
          return clients;
      }
  
      public void setClients(List&lt;Client&gt; clients) {
          this.clients = clients;
      }
  
      ...
  
  }
  </code></pre>
  
  <p>After doing some research it looks like the only solution is for <a href="http://en.wikibooks.org/wiki/Java_Persistence/ManyToMany#Mapping_a_Join_Table_with_Additional_Columns" rel="nofollow noreferrer">mapping a join table with additional columns</a> using <code>@OneToMany</code> <code>@IdClass</code> and a composite primary key class when I don't need additional columns.  Is this the only solution besides the one I've included in the code above, which is using a <code>@UniqueConstraint</code> with the two foreign key columns on the <code>@ManyToMany</code> mapping?  It seems a bit ridiculous the amount of work needed for a common scenario like this.  Thanks!</p>