如何将两个FK之间的列共享到同一个参考表? 我有四个实体:玩家,团队,TeamPlayer和PlayerScore。
现在是用例:
每个击球手在板球比赛中(对于一个非全球范例而言)为一支特定球队效力的球员将在他有一名叫做跑步者的伙伴击球手时得分。现在, PlayerScore 实体需要捕获此信息。 所以,我必须确保击球手和他的搭档都在为同一支球队效力。我可以用这张表来了解哪对击球手表现最好。确切地说,我需要从PlayerScore Entity到TeamPlayer实体的两个引用。他们俩只分享一个专栏,团队。我怎样才能做到这一点?
以下是四个类:
@Entity
@Table(name="team")
public class Team {
@Id
private int id;
@Column(name="name",length=50)
private String name;
}
@Entity
@Table(name="player")
public class Player {
@Id
private int id;
@Column(name="name",length=50)
private String name;
}
@Entity
@Table(name="team_player")
public class TeamPlayer {
@EmbeddedId
private TeamPlayerPK id;
@ManyToOne(targetEntity=Player.class)
@JoinColumn(name="player")
private Player player;
@ManyToOne(targetEntity=Team.class)
@JoinColumn(name="team")
private Team team;
@Column(name="name",length=50)
private String name;
@Embeddable
public static class TeamPlayerPK implements Serializable
{
private static final long serialVersionUID = 1L;
private int team;
private int player;
}
}
@Entity
@Table(name="player_score")
public class PlayerScore {
@Id
private int scoreId;
@ManyToOne(targetEntity=TeamPlayer.class)
@JoinColumns(value={@JoinColumn(name="team",referencedColumnName="team"),@JoinColumn(name="batsmen",referencedColumnName="player")})
private TeamPlayer batsman;
@ManyToOne(targetEntity=TeamPlayer.class)
@JoinColumns(value={@JoinColumn(name="team",referencedColumnName="team"),@JoinColumn(name="runner",referencedColumnName="player")})
private TeamPlayer runner;
private int score;
@Temporal(TemporalType.DATE)
private Date matchDate;
}
编辑1:添加了评论
中建议的Mysql WB模型编辑2:首次尝试失败:
团队和玩家实体仍然如上所述。但是TeamPlayer已经改变如下:
@ManyToOne(targetEntity=Player.class)
@PrimaryKeyJoinColumn(name="player",referencedColumnName="id")
private Player player;
@JoinColumn已更改为@PrimaryKeyJoinColumn
PlayerScore实体中跑步者字段的注释更改如下:
@ManyToOne(targetEntity=TeamPlayer.class)
@JoinColumns(value={@JoinColumn(name="team",referencedColumnName="team",insertable=false,updatable=false),@JoinColumn(name="runner",referencedColumnName="player",insertable=true,updatable=true)})
private TeamPlayer runner;
期望是还会生成跑步者的FK参考。代码编译和Eclipselink通过代,但没有生成转轮的外键。寻求成功......