title: JPA/Hibernate mapping table for One-to-many relationships
body: <p>I'm using JPA annotations only and hibernate 4.0.1 as the JPA implementation.</p>
  
  <p>I have an<code>Assessment</code>class that has two sets of <code>Comment</code>:</p>
  
  <pre><code>@Entity
  @Table(name = "ASSESSMENT")
  public class Assessment{
  
      @OneToMany(fetch = FetchType.EAGER)
      private Set&lt;Comment&gt; auditComments = new HashSet&lt;Comment&gt;();
  
      @OneToMany(fetch = FetchType.EAGER)
      private Set&lt;Comment&gt; comments = new HashSet&lt;Comment&gt;();
  }
  </code></pre>
  
  <p><em>(I'm skipping getters, setters, cascade annotations and other irrelevant lines of code)</em></p>
  
  <p>Comment:</p>
  
  <pre><code>@Entity
  @Table(name = "COMMENT")
  public class Comment{
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Long id;
  
      @Column(columnDefinition = "text")
      private String text;
  }
  </code></pre>
  
  <p>Now, because a <code>Comment</code> doesn't have a reference back to <code>Assessment</code> (this is on purpose), I cannot uset the <em>mappedBy</em> property and Hibernate needs to create a mapping table between <code>Comment</code> and <code>Assessment</code>. By default, it is named ASSESSMENT_COMMENT.</p>
  
  <p>If I had only one set of <code>Comment</code> in <code>Assessment</code>, that would work perfectly fine. The ASSESSMENT_COMMENT would have only two columns:</p>
  
  <pre><code>[ASSESSMENT_ID, COMMENT_ID]
  </code></pre>
  
  <p>which will perfectly represent the one-to-many relationship.</p>
  
  <p><strong>Now here is the problem:</strong></p>
  
  <p>Because I don't have one set of <code>Comment</code> but two, Hibernate tries to map the two sets using only one ASSESSMENT_COMMENT table. This table has 3 columns:</p>
  
  <pre><code>[ASSESSMENT_ID, COMMENT_ID, AUDITCOMMENT_ID]
  </code></pre>
  
  <p>On top of that, the 3 columns are set as non nullable.</p>
  
  <p>That certainly cannot work. For instance, if I have only one item in auditComents and  none in  comments, hibernate tries to insert one row with a null in COMMENT_ID which creates a SQL exception.</p>
  
  <p><strong>Questions:</strong></p>
  
  <p>To me it looks like a bug. There should be two mapping tables, not one.</p>
  
  <p>So, 1) is it a known bug in Hibernate? 2) is there a way around that? Can I force Hibernate to create two mapping tables, one for each of the set. Bear  in mind that I cannot change the <code>Comment</code> class to have a reference to <code>Assessment</code> (business logic requirement)</p>