我有两个类Game和Team,它们之间有多对多的关系。
游戏:
@Entity
@Table(name = "GAME")
public class Game extends Model {
@ManyToMany(cascade = {CascadeType.ALL})
@JoinTable(name="GAME_TEAM",
joinColumns={@JoinColumn(name="GAME_ID")},
inverseJoinColumns={@JoinColumn(name="TEAM_ID")})
private List<Team> teams = new ArrayList<Team>();
public List<Team> getTeams() {
return teams;
}
public void setTeams(List<Team> teams) {
this.teams = teams;
}
}
小组:
@Entity
@Table(name = "TEAM")
public class Team extends Model {
@Column(name = "TEAM_NAME")
private String teamName;
@OneToMany(mappedBy = "team", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
private Set<Player> player = new LinkedHashSet<Player>();
@ManyToMany(cascade=CascadeType.ALL, mappedBy="teams")
private Set<Game> games;
public Set<Player> getPlayer() {
return player;
}
public void setPlayer(Set<Player> player) {
this.player = player;
}
public Set<Game> getGames()
{
return games;
}
public void setGames(Set<Game> games) {
this.games = games;
}
public String getTeamName() {
return teamName;
}
public void setTeamName(String teamName) {
this.teamName = teamName;
}
在这两个类GAME_TEAM之间有第三个中间表,它有team_id,game_id是来自游戏和团队类的ID的外键。因此,每当我更新团队和游戏时,我都会自动更新game_team。
确定。好。现在问题是什么?我有第三列WON_BY,也需要更新。如果没有这个额外的第三列,我就不必创建第三类GameTeam。
要更新非外键的第三列,是否应该为中间表创建实体类?如果是这样,我应该使用什么样的方法或注释?
答案 0 :(得分:3)
你需要一个单独的实体类来保持你的'WON_BY',和 你需要重新考虑现有的实体类,因为 解决方案相当大,请参考下面的示例示例 绝对能解决你的问题。