[英]How do I create a JoinTable which includes both a primitive and a composite primary key as the tables composite key (Spring JPA)?
I have a Users, Players, CoopGames, and CoopGamesPlayers tables.我有一个用户、玩家、CoopGames 和 CoopGamesPlayers 表。 Conceptually, I am essentially trying to recreate the Order/OrderProducts type relationship.
从概念上讲,我基本上是在尝试重新创建 Order/OrderProducts 类型关系。 In my case, every CoopGamesPlayers entry has as its key the CoopGame id and the Player id.
就我而言,每个 CoopGamesPlayers 条目都将 CoopGame id 和 Player id 作为其键。 However, I am stuck because in my case my Player class has a composite primary key itself: generated player id, and also User id foreign key.
但是,我被卡住了,因为在我的情况下,我的 Player 类本身有一个复合主键:生成的玩家 ID,以及用户 ID 外键。 I am kind of lost.
我有点失落。 I have set this up following a tutorial for Order.OrderProducts but I am not a JPA expert and have never really used composite keys in a JoinTable.
我按照 Order.OrderProducts 的教程进行了设置,但我不是 JPA 专家,也从未真正在 JoinTable 中使用过复合键。 Code:
代码:
public class PlayerPK implements Serializable {
@ManyToOne(optional = false, fetch = FetchType.EAGER)
@JoinColumn(name = "id")
private Player player;
@ManyToOne(optional = false, fetch = FetchType.EAGER)
@JoinColumn(name = "userid")
private User user;
}
@Entity
@Table(name = "players")
public class Player implements Serializable {
@EmbeddedId
private PlayerPK pk;
@Column(name = "image")
private String image;
}
@Entity
@Table(name = "coop_games")
public class CoopGame {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@OneToMany(mappedBy = "pk", fetch = FetchType.EAGER)
private List<CoopGamesPlayers> players;
}
public class CoopGamesPlayersPK implements Serializable {
@JsonBackReference
@ManyToOne(optional = false, fetch = FetchType.EAGER)
@JoinColumn(name = "coop_game_id")
private CoopGame game;
@ManyToOne(optional = false, fetch = FetchType.EAGER)
@JoinColumns({
@JoinColumn(name="playerid", referencedColumnName="id"),
@JoinColumn(name="userid", referencedColumnName="userid")
})
@MapsId("pk")
private Player player;
}
@Entity
@Table(name = "coop_games_players")
public class CoopGamesPlayers {
@EmbeddedId
private CoopGamesPlayersPK pk;
public CoopGamesPlayers(CoopGame game, Player player) {
pk = new CoopGamesPlayersPK();
pk.setGame(game);
pk.setPlayer(player);
}
}
@Entity
@Table(name = "coop_games")
public class CoopGame {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@OneToMany(mappedBy = "pk", fetch = FetchType.EAGER)
private List<CoopGamesPlayers> players;
}
I think you should better rethink your data model with all relationships (Which problem should be solved? ..).我认为您应该更好地重新考虑所有关系的数据模型(应该解决哪个问题?..)。
As for your CoopGamesPlayers Table: if you just want to use CoopGamesPlayers as a jointable, you don't need the CoopGamesPlayersPK.至于您的 CoopGamesPlayers 表:如果您只想将 CoopGamesPlayers 用作可连接对象,则不需要 CoopGamesPlayersPK。
CoopGame und CoopGamesPlayers should look something like this: CoopGame 和 CoopGamesPlayers 应如下所示:
@Entity
@Table(name = "coop_games")
public class CoopGame {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@OneToMany(mappedBy = "game", fetch = FetchType.EAGER)
private List<CoopGamesPlayers> players;
}
@Entity
@Table(name = "coop_games_players")
public class CoopGamesPlayers {
@EmbeddedId
private PlayerPK pk;
@JsonBackReference
@ManyToOne(optional = false, fetch = FetchType.EAGER)
@JoinColumn(name = "coop_game_id")
private CoopGame game;
@ManyToOne(optional = false, fetch = FetchType.EAGER)
@JoinColumns({
@JoinColumn(name = "playerid", referencedColumnName = "id"),
@JoinColumn(name = "userid", referencedColumnName = "userid")
})
@MapsId("id")
private Player player;
}
@MapsId("id")
- id because you want player id as fk or? @MapsId("id")
- id 因为你想要玩家 id 作为 fk 还是?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.