title: Is @ManyToMany(mappedBy = ... ) + @OrderColumn supported by the JPA?
body: <p>I have an entity mapped as following:</p>
  
  <pre><code>@Entity
  @Table(name = "Groups")
  @IdClass(value = GroupId.class)
  public class Group implements Serializable
  {
      @Id
      @Column(name = "round_id")
      private Integer roundId;
  
      @Id
      @Column(name = "ordinal_nbr")
      private Integer ordinalNbr = 0;
  
      ...
  }
  </code></pre>
  
  <p>It has a composite key <code>(round_id, ordinal_nbr)</code> to indicate the order of groups in a round.</p>
  
  <p>Now imagine a join table containing entities that link the ordered groups via a self reference (from Group to Group):</p>
  
  <pre><code>CREATE TABLE GroupLinks
  (
    parent_round_id INTEGER NOT NULL,
    parent_ordinal_nbr SMALLINT NOT NULL,
    child_round_id INTEGER NOT NULL,
    child_ordinal_nbr SMALLINT NOT NULL,
    PRIMARY KEY (parent_round_id, parent_ordinal_nbr, child_round_id, child_ordinal_nbr),
    FOREIGN KEY (parent_round_id, parent_ordinal_nbr) REFERENCES Groups (round_id, ordinal_nbr),
    FOREIGN KEY (child_round_id, child_ordinal_nbr) REFERENCES Groups (round_id, ordinal_nbr)
  );
  </code></pre>
  
  <p>I know it's possible to map <code>@ManyToMany</code> + <code>@JoinTable</code> + <code>@OrderColumn</code> for the owning entity (whichever I choose):</p>
  
  <pre><code>@ManyToMany
  @JoinTable(name = "GroupLinks", joinColumns = {@JoinColumn(name = "parent_round_id", referencedColumnName = "round_id"), @JoinColumn(name = "parent_ordinal_nbr", referencedColumnName = "ordinal_nbr")}, inverseJoinColumns = {@JoinColumn(name = "child_round_id", referencedColumnName = "round_id"), @JoinColumn(name = "child_ordinal_nbr", referencedColumnName = "ordinal_nbr")})
  @OrderColumn(name = "child_ordinal_nbr")
  private List&lt;Group&gt; children;
  </code></pre>
  
  <p><strong>Question:</strong></p>
  
  <p>For the owned side, is it supported to map the <code>@ManyToMany(mappedBy = ...)</code> inverse relationship with an <code>@OrderColumn</code>, too?</p>
  
  <pre><code>@ManyToMany(mappedBy = "parents")
  @OrderColumn(name = "parent_ordinal_nbr")
  private List&lt;Group&gt; parents;
  </code></pre>
  
  <p>Does the JPA 2 spec define to allow or deny this?</p>
  
  <p>Thanks</p>
  
  <hr>
  
  <p><strong>Update 1:</strong></p>
  
  <p>The above is essentially a graph structure. Here's an example:</p>
  
  <p><img src="https://i.stack.imgur.com/QHQqD.png" alt="IMAGE"></p>
  
  <p>The <code>GroupLinks</code> table contains the boxed entities. When looking at the B2 <code>Group</code> entity only, the <code>parents</code> list will contain <code>(a,1,b,2)</code> and <code>(a,2,b,2)</code>. As for the <code>children</code> list, it will contain <code>(b,2,c,1)</code>, <code>(b,2,c,2)</code>, and <code>(b,2,c,3)</code>.</p>
  
  <p>As you can see the entities in the lists of <code>B2</code> won't collide, so an <code>@OrderColumn</code> should work okay for <strong>both relationships</strong> <code>parents</code> and <code>children</code> - at least in theory. But what's the practice here (JPA)?</p>
  
  <p>Note, that simply trying it on Hibernate and/or EclipseLink doesn't really answer the question whether the JPA 2 spec or a JPA-compatible provider should or must support this scenario.</p>
  
  <hr>
  
  <p><strong>Update 2:</strong></p>
  
  <p>Trying the above mappings on Hibernate results in the following mapping exception:</p>
  
  <pre><code>Caused by: org.hibernate.MappingException: Repeated column in mapping for collection: com.kawoolutions.bbstats.model.Group.parents column: parent_ordinal_nbr
      at org.hibernate.mapping.Collection.checkColumnDuplication(Collection.java:340)
      at org.hibernate.mapping.Collection.checkColumnDuplication(Collection.java:363)
      at org.hibernate.mapping.Collection.validate(Collection.java:320)
      at org.hibernate.mapping.IndexedCollection.validate(IndexedCollection.java:89)
      at org.hibernate.cfg.Configuration.validate(Configuration.java:1291)
      at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1729)
      at org.hibernate.ejb.EntityManagerFactoryImpl.&lt;init&gt;(EntityManagerFactoryImpl.java:84)
      at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:904)
      ... 9 more
  </code></pre>
  
  <p>Removing the <code>@OrderColumn</code> for <code>parent_ordinal_nbr</code> results in the same exception for the <code>children</code> relationship. It looks like Hibernate doesn't like order columns which are also used in foreign keys.</p>
  
  <hr>
  
  <p><strong>Update 3:</strong></p>
  
  <p>Tested with EclipseLink on GlassFish... this works, no mapping exceptions.</p>
  
  <p>This raises two followup questions:</p>
  
  <ol>
  <li><strong>Does the JPA disallow foreign key order columns?</strong> It doesn't make sense to me why they shouldn't simply work...</li>
  <li>Is this a Hibernate bug?</li>
  </ol>