在Hibernate中的表之间指定多个一对多关系 [英] Specifying multiple one-to-many relationships between tables in Hibernate
问题描述
我有两个表:团队和比赛
I have two tables: Teams and Matches
团队"表的组成如下[团队ID,团队名称] 比赛表由以下内容组成:[比赛ID,主队,客队,结果]
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?
如何在Hibernate中指定这些关系?
How can these relationships be specified in Hibernate?
推荐答案
您可以在小组和比赛之间建立单向映射,如下所示:
You can have a uni-directional mapping between Teams and Matches as follows:
在比赛类别中:
@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 :
在团队课中:
@OneToMany(mappedBy = "homeTeam")
private Set<Matches> homeMatches;
@OneToMany(mappedBy = "awayTeam")
private Set<Matches> awayMatches;
这篇关于在Hibernate中的表之间指定多个一对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!