I'm trying to map these two entitiy tables to one model class, game and platform. A game can have multiple platforms. So in my db I have the following tables:
CREATE TABLE IF NOT EXISTS games (
id SERIAL NOT NULL PRIMARY KEY,
name VARCHAR,
summary TEXT,
avg_score REAL,
);
CREATE TABLE IF NOT EXISTS platforms (
id SERIAL NOT NULL PRIMARY KEY,
name VARCHAR
);
And a relationship table called Game_Platforms
CREATE TABLE IF NOT EXISTS game_platforms (
id serial not null primary key,
game_id INTEGER NOT NULL,
platform_id INTEGER NOT NULL,
release_date date not null,
FOREIGN KEY (game_id) REFERENCES games (id) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (platform_id) REFERENCES platforms (id) ON DELETE CASCADE ON UPDATE CASCADE,
UNIQUE(game_id,platform_id, release_date) --The same game can be released for the same platform multiple times (i.e. remaster)
);
Note that the tables have more columns but I'm just showing the ones that are relevant to this problem.
So on my model I have the following class which I want to map to the db using JPA
@Entity
@Table(name = "games")
public class Game {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "games_gameid_seq")
private long id;
@Column(length = 100, nullable = false, unique = true)
private String name;
//Map with platforms
private Map<String, LocalDate> platforms;
And platform which doesn't have a class because I didn't find it necessary. I don't think a @OneToMany annotation will be enough to map this. Adding a class "Platform" to my model would be of last resort as I would have to change most of my interfaces, needing to change the whole app. Any idea on how to aproach this? Thanks in advance!