Sharing one column in multiple ForeignKeys [message #1767660] |
Sat, 08 July 2017 17:22 |
Venkata Rahul Messages: 1 Registered: July 2017 |
Junior Member |
|
|
I have posted this question on Stackoverflow and am yet to see a solution. Let me post the same question today.
How can I share a column between two FKs to the same reference table? I have four entities: Player,Team, TeamPlayer and PlayerScore.
Now here is the use case:
Every batsman in cricket (sorry for a non-global example) playing for a specific team will be scoring when he has a partner-batsman called the runner. Now, the PlayerScore entity needs to capture this information. So, I must ensure that both the batsman and his partner are playing for the same team. I can use this table to understand which pairs of batsman have been the performing the best. In exact terms, I need two references from PlayerScore Entity to the TeamPlayer entity. Both of them share exactly one column, team. How can I achieve this?
Here are the four classes:
@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;
}
Can you help me please?
Thank you,
Rahul
|
|
|
|
Powered by
FUDForum. Page generated in 0.02217 seconds