[英]Specifying multiple one-to-many relationships between tables in Hibernate
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] 团队表由以下组成[Team ID,Team Name]比赛表由以下组成[Match ID,主队,客队,结果]
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? 在Hibernate中如何指定这些关系?
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.