来自我的Game.java的片段:
@Entity
public class Game extends Model{
@Id
public Long id;
@ManyToOne
public User user;
@ManyToMany(cascade = CascadeType.ALL)
public List<Checkpoint> visitedCheckpoints = new ArrayList<Checkpoint>();
@ManyToMany(cascade = CascadeType.ALL)
public List<Checkpoint> answeredCheckpoints = new ArrayList<Checkpoint>();
}
在自动SQL演变中,Play只创建了一个多对多关系表:game_checkpoint
,只有两列是外键:game_id
和checkpoint_id
。这会导致错误:我添加了一个访问检查点,但由于主键约束违规,我无法添加相同的检查点,因为主键约束违规。
如何强制Play使用两个表生成SQL:game_visitedcheckpoint
和game_answeredcheckpoint
以后我可以单独操作两个检查点列表?
答案 0 :(得分:1)
通过明确定义表名来解决这个问题:
@Entity
public class Game extends Model{
@Id
public Long id;
@ManyToOne
public User user;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name="game_visitedcheckpoint")
public List<Checkpoint> visitedCheckpoints = new ArrayList<Checkpoint>();
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name="game_answeredcheckpoint")
public List<Checkpoint> answeredCheckpoints = new ArrayList<Checkpoint>();
}