How can I share a column between two FKs to the same reference table? [message #1804719] |
Sat, 30 March 2019 02:47  |
Eclipse User |
|
|
|
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. dqfanfeedback
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,
[Updated on: Tue, 02 April 2019 03:52] by Moderator
|
|
|
|
|
Re: How can I share a column between two FKs to the same reference table? [message #1830275 is a reply to message #1830262] |
Wed, 22 July 2020 10:06  |
Eclipse User |
|
|
|
To clarify you essentially want to know how to map:
create table player_score (
...
player_1 references player (id),
player_2 references player (id),
team references team (id),
primary key (player_1, player_2, team)
);
as you have a class/table already:
create table team_player (
...
player references player (id),
team references team (id),
primary key(player, team)
);
You want to map the EmbeddedId you've created for the primary key in the TeamPlayer object to two in the PlayerScore object.
I think you'd achieve (not tested) with
@EmbeddedId
@AttributeOverride(name="player", @Column(name="player_1"))
TeamPlayerPK teamPlayer1;
@EmbeddedId
@AttributeOverride(name="player", @Column(name="player_2"))
TeamPlayerPK teamPlayer2;
e.g. see https://stackoverflow.com/questions/36571347/jpa-how-to-override-column-names-of-embedded-attributes/36571484
[Edit: re-read you were already suggesting using just 1 team pkey.]
[Updated on: Wed, 22 July 2020 17:57] by Moderator
|
|
|
Powered by
FUDForum. Page generated in 0.03251 seconds