Reputation: 1192
I have two tables: Teams and Matches
The Teams table is made up as follows [Team ID, Team Name] The Matches table is made up as follows [Match ID, Home Team, Away Team, Result]
Home Team and Away Team are foreign keys from the Teams table therefore there are two one-to-many relationships defined between the Teams and Matches table.
I'm know how to specify single relationships between two tables but how can I specify multiple relationships between two tables? Is it the same process?
How can these relationships be specified in Hibernate?
Upvotes: 1
Views: 9403
Reputation: 1019
You can have a uni-directional mapping between Teams and Matches as follows:
In Matches Class:
@ManyToOne(optional = false)
@JoinColumn(name = "home_team_id", referencedColumnName = "team_id")
private Team homeTeam;
@ManyToOne(optional = false)
@JoinColumn(name = "away_team_id", referencedColumnName = "team_id")
private Team awayTeam;
If you need a bi-directional relationship you can add the following :
In Teams Class :
@OneToMany(mappedBy = "homeTeam")
private Set<Matches> homeMatches;
@OneToMany(mappedBy = "awayTeam")
private Set<Matches> awayMatches;
Upvotes: 7