title: Modelling a Two to Many Relationship in JPA/Hibernate
body: <p>I have the following entity relationship problem. A "Game" must have two (and only two) "Team" objects. A "Team" can have many "Games"</p>
  
  <p>This, as far as I can see is a Two-to-Many relationship. However...I don't know how to model this in JPA. Eg, I was going to do something like this...</p>
  
  <pre><code>@Entity
  public class Team extends BaseObject {
    private Long id;
    private Set&lt;Game&gt; games;
  
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO) 
    public Long getId() {return id;}
    public void setId(Long id) {this.id = id;}
  
    @OneToMany(mappedBy = "game")
    public Set&lt;Game&gt; getGames() {return games;}
    public void setGames(Set&lt;Game&gt; games) {this.games = games;}
  }
  
  @Entity
  public class Game extends BaseObject {
    private Long id;
    private Team team1;
    private Team team2;
  
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO) 
    public Long getId() {return id;}
    public void setId(Long id) {this.id = id;}
  
    @ HERE IS THE PROBLEM - WHAT ANNOTATION DO I USE?
    public Team getTeam1() {return team1;}
    public void setTeam1(Team team1) {this.team1 = team1;}
  
    @ HERE IS THE PROBLEM - WHAT ANNOTATION DO I USE?
    public Team getTeam2() {return team2;}
    public void setTeam2(Team team1) {this.team2 = team2;}
  }
  </code></pre>
  
  <p>But, as you can see, I'm not sure how to link the tables together from an annotation side. Has anyone ever done something like this before? Any ideas, help?</p>
  
  <p>thanks very much!</p>