С учетом этих лиц:
class SportTeam {
@Id
@GeneratedValue
long id;
@OneToMany
private Set<PLayer> players;
@OneToMany
private Set<Player> stars;
}
// A sport team can have multiple players and some of those players can be stars.
class Player {
@Id
@GeneratedValue
long id;
(...)
}
Вот DDL, сгенерированный Hibernate:
CREATE TABLE sportteam
(
id bigint NOT NULL,
(...)
CONSTRAINT sportteam_pkey PRIMARY KEY (id)
)
CREATE TABLE sportteam_player
(
sportteam_id bigint NOT NULL,
player_id bigint NOT NULL,
star_id bigint NOT NULL,
CONSTRAINT sportteam_player_pkey PRIMARY KEY (sportteam_id, star_id),
CONSTRAINT fk6cf55c6645d973bc FOREIGN KEY (player_id)
REFERENCES player (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT fk6cf55c66ca1af8b8 FOREIGN KEY (star_id)
REFERENCES player (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT sportteam_player_star_id_key UNIQUE (star_id),
CONSTRAINT sportteam_player_player_id_key UNIQUE (player_id)
)
CREATE TABLE player
(
id bigint NOT NULL,
(...)
CONSTRAINT player_pkey PRIMARY KEY (id)
)
Я бы предпочел, чтобы sportteam_player выглядел так:
CREATE TABLE sportteam_player
(
sportteam_id bigint NOT NULL,
player_id bigint NOT NULL,
is_star boolean DEFAULT 'FALSE',
CONSTRAINT sportteam_player_pkey PRIMARY KEY (sportteam_id, player_id),
CONSTRAINT fk6cf55c6645d973bc FOREIGN KEY (player_id)
REFERENCES player (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT fk6cf55c66ca1af8b8 FOREIGN KEY (sportteam_id)
REFERENCES sportteam (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
Как мне это сделать?