title: JPA: Share one column among multiple FK mappings
body: <p>How can I share a column between two FKs to the same reference table?
  I have four entities: Player,Team, TeamPlayer and PlayerScore.</p>
  
  <p>Now here is the use case:</p>
  
  <p>Every <em>batsman</em> in cricket (sorry for a non-global example) playing for a specific team will be scoring when he has a partner-batsman called the <em>runner</em>. Now, the <em>PlayerScore</em> entity needs to capture this information. 
  So, I must ensure that both the batsman and his partner are playing for the same team. I can use this table to understand which pairs of batsman have been the performing the best. In exact terms, I need two references from PlayerScore Entity to the TeamPlayer entity. Both of them share exactly one column, team. How can I achieve this?</p>
  
  <p>Here are the four classes:</p>
  
  <pre><code>@Entity
  @Table(name="team")
  public class Team {
  
      @Id
      private int id;
  
      @Column(name="name",length=50)
      private String name;
  }
  
  @Entity
  @Table(name="player")
  public class Player {
  
      @Id
      private int id;
  
      @Column(name="name",length=50)
      private String name;
  }
  
  @Entity
  @Table(name="team_player")
  public class TeamPlayer {
  
      @EmbeddedId
      private TeamPlayerPK id;
  
      @ManyToOne(targetEntity=Player.class)
      @JoinColumn(name="player")
      private Player player;
  
      @ManyToOne(targetEntity=Team.class)
      @JoinColumn(name="team")
      private Team team;
  
      @Column(name="name",length=50)
      private String name;
  
  @Embeddable
  public static class TeamPlayerPK implements Serializable
  {
  
      private static final long serialVersionUID = 1L;
  
      private int team;
      private int player;
  }
  }
  
  @Entity
  @Table(name="player_score")
  public class PlayerScore {
  
      @Id
      private int scoreId;
  
      @ManyToOne(targetEntity=TeamPlayer.class)
      @JoinColumns(value={@JoinColumn(name="team",referencedColumnName="team"),@JoinColumn(name="batsmen",referencedColumnName="player")})
      private TeamPlayer batsman;
  
      @ManyToOne(targetEntity=TeamPlayer.class)
      @JoinColumns(value={@JoinColumn(name="team",referencedColumnName="team"),@JoinColumn(name="runner",referencedColumnName="player")})
      private TeamPlayer runner;
  
      private int score;
  
      @Temporal(TemporalType.DATE)
      private Date matchDate;
  
  }
  </code></pre>
  
  <p>EDIT 1: Added the Mysql WB model as suggested in the comment</p>
  
  <p>EDIT 2: First unsuccessful attempt:</p>
  
  <p>The Team, and Player entities remain as above. But the TeamPlayer has been changed as follows:</p>
  
  <pre><code>@ManyToOne(targetEntity=Player.class)
  @PrimaryKeyJoinColumn(name="player",referencedColumnName="id")
  private Player player;
  </code></pre>
  
  <p>The @JoinColumn has been changed to @PrimaryKeyJoinColumn</p>
  
  <p>The annotations for runner field in the PlayerScore entity is changed as follows:</p>
  
  <pre><code>@ManyToOne(targetEntity=TeamPlayer.class)
  @JoinColumns(value={@JoinColumn(name="team",referencedColumnName="team",insertable=false,updatable=false),@JoinColumn(name="runner",referencedColumnName="player",insertable=true,updatable=true)})
  private TeamPlayer runner;
  </code></pre>
  
  <p>The expectation is that the FK reference for runner is also generated. THe code compiles and Eclipselink goes thru the generation but the foreign key for runner is NOT generated. In search of success yet...</p>
  
  <p><a href="https://i.stack.imgur.com/l3az3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/l3az3.png" alt="enter image description here"></a></p>