我是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。我的问题在哪里?
答案 0 :(得分:0)
删除PrimaryKeyJoinColumn注释,因为我认为它不是你想要使用的,因为它与joincolumn定义冲突。请使用joincolumn批注来定义外键字段名称及其引用的字段(如果需要)。