我是 JPA 的新手,并且努力定义我的类之间的关系。我有一个名为 Player 的类和一个名为 Game 的类。一个游戏持有对两个 Player 实例的引用。问题是,这应该如何建模?
这是我当前的代码:
@Entity
@Table(name = "t_player")
@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
public class Player {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Basic
@Column(name = "name")
private String name;
@Basic
@Column(name = "uuid")
private final String uuid = UUID.randomUUID().toString();
我认为这没问题,但我的问题出在 Game 类中:
@Entity
@Table(name = "t_game")
@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
public class Game {
public Game() {
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Basic
@Column(name = "uuid")
private final String uuid = UUID.randomUUID().toString();
@OneToOne
@PrimaryKeyJoinColumn
@JoinColumn(name = "id")
private Player player_1;
@OneToOne
@PrimaryKeyJoinColumn
@JoinColumn(name = "player_2")
private Player player_2;
public Game(Player player_1, Player player_2) {
this.player_1 = player_1;
this.player_2 = player_2;
}
}
这不起作用,我的表 t_game 只有两个字段;id 和 uuid。我的问题在哪里?