问题描述:

A fragment from my 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>();

}

In the automatic SQL evolution, Play created just one many-to-many relationship table: game_checkpoint, with just two columns being foreign keys: game_id and checkpoint_id. That leads to an error: I add a visited checkpoint, but then I can't add the same checkpoint as answered, because of primary key constraint violation.

How to force Play to generate SQL with two tables: game_visitedcheckpoint and game_answeredcheckpoint so later I can manipulate the two Checkpoint lists separately?

网友答案:

Solved this by defining tables' names explicitly:

@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>();
}
相关阅读:
Top