Sharing one column in multiple ForeignKeys [message #1767660] |
Sat, 08 July 2017 13:22  |
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. 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
|
|
|
Re: Sharing one column in multiple ForeignKeys [message #1767750 is a reply to message #1767660] |
Mon, 10 July 2017 11:27  |
Eclipse User |
|
|
|
I don't understand the point of the TeamPlayer class. A team seems like it needs either a ManyToMany through the team_player table or OneToMany relationship relationship if a player can only play for one team at a time. A PlayerScore then seems like it should reference two players and the team, not the team_player table - this does assume they play for the same 'team' though, which you've stated only has the 1 foreign key for anyway.
If you really want to set up two references to TeamPlayer and have both references us the same 'team' foreign key, you will have to mark one of them as insertable=false, updateable=false. And if you want to keep the TeamPlayer entity, you'll need to fix its mappings as you have two using the "player" and "team" foreign keys. Depending on how you want to set the value of this foreign key (manually or let JPA handle it for you) check out derrived ID support in JPA2.0. This allows you to mark the reference mapping with the @Id:
@Entity
@Table(name="team_player")
@IdClass(TeamPlayerPK.class)
public class TeamPlayer {
@Id
@ManyToOne(targetEntity=Player.class)
@JoinColumn(name="player")
private Player player;
@Id
@ManyToOne(targetEntity=Team.class)
@JoinColumn(name="team")
private Team team;
@Column(name="name",length=50)
private String name;
}
See https://wiki.eclipse.org/EclipseLink/Examples/JPA/2.0/DerivedIdentifiers
|
|
|
Powered by
FUDForum. Page generated in 0.02879 seconds