title: JPA: Multiple many-to-many relations between two entities?
body: <p>I have two entity classes 'User' and 'Document'. Each user has an inbox and an outbox which are in fact two List and each Document may reside in multiple inbox's and outbox's of users. Here are my classes:</p>
  
  <pre><code>@Entity
  public class User {
  
      @Id
      private Long id;
  
      @ManyToMany(mappedBy = "userinbox", cascade=CascadeType.ALL)
      private List&lt;Document&gt; inbox = new ArrayList&lt;Document&gt;();
      @ManyToMany(mappedBy = "useroutbox", cascade=CascadeType.ALL)
      private List&lt;Document&gt; outbox = new ArrayList&lt;Document&gt;();
  
  }
  
  @Entity
  public class Document {
  
      @Id
      private Long id;
  
      @ManyToMany(cascade=CascadeType.ALL)
      private List&lt;User&gt; userinbox  = new ArrayList&lt;User&gt;();
      @ManyToMany(cascade=CascadeType.ALL)
      private List&lt;User&gt; useroutbox  = new ArrayList&lt;User&gt;();
  
  }
  </code></pre>
  
  <p>When I run the programm and try to assign a document to a user's inbox (and vice-versa), I get the following error:</p>
  
  <pre><code>Error Code: 1364
  
  Call: INSERT INTO DOCUMENT_USER (userinbox_ID, inbox_ID) VALUES (?, ?)
      bind =&gt; [2 parameters bound]
  
  Internal Exception: java.sql.SQLException: Field 'useroutbox_ID' doesn't have a default value
  
  Query: DataModifyQuery(name="userinbox" sql="INSERT INTO DOCUMENT_USER (userinbox_ID, inbox_ID) VALUES (?, ?)")
  </code></pre>
  
  <p>The generated association table looks like this:</p>
  
  <pre><code>DOCUMENT_USER
  useroutbox_ID | outbox_ID |userinbox_ID | inbox_ID
  </code></pre>
  
  <p>How would I assign default values for such a many-to-many relation? Would it be better to make two association tables -> one for inbox-relation, another for outbox-relation? How would I accomplish that ? Other solutions to this problem ?</p>
  
  <p>Any help highly appreciated - many thanks in advance!</p>