title: JPA/Hibernate: Map many-to-many relationship when join table has own primary key
body: <p>I have three tables with simple structure:</p>
  
  <pre><code>pub [id, name]
  days [id, name]
  pub_days [id, pub_id, days_id]
  </code></pre>
  
  <p>For some unholy reason, somebody thought that compound identity for <strong>pub_days</strong> table (that would be pub_id + days_id) is not enough and added own primary key. I can't change it now, other and larger system depends on that. #sigh</p>
  
  <p>I am trying to map this to Hibernate with standard <code>@ManyToMany</code> JPA annotation like so (I omitted getters, setters, @Entitiy annotations and other clutter):</p>
  
  <pre><code>class Pub {
  
      @ManyToMany(cascade = {CascadeType.ALL})
      @JoinTable(name = "pub_days",
              joinColumns = {@JoinColumn(name = "pub_id")},
              inverseJoinColumns = {@JoinColumn(name = "days_id")})
      @OrderBy("id")
      private List&lt;Day&gt; pubOpeningDays;
  }
  
  class Day {
      @Id Long id;
      String name.
  }
  </code></pre>
  
  <p>when I execute following code:</p>
  
  <pre><code>Day day = repository.find(Day.class, id);
  pub.getPubOpeningDays().add(day);
  repository.persist(pub);
  </code></pre>
  
  <p>I get this error:</p>
  
  <pre><code>ERROR: ORA-01400: cannot insert NULL into ("PUB"."pub_days"."id")
  </code></pre>
  
  <p>Sadly, that makes perfect sense, because I haven't mapped that ID anywhere. The thing is, I don't even want to. I want it to be generated, but not sure how do I overcome this issue with <code>@ManyToMany</code> mapping. Any ideas?</p>