pmichna
pmichna

Reputation: 4888

Play Framework generated one table for two Many To Many relations

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?

Upvotes: 0

Views: 908

Answers (1)

pmichna
pmichna

Reputation: 4888

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>();
}

Upvotes: 1

Related Questions