1
鑑於這些實體:JPA - 表錯誤創建
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;
(...)
}
這裏產生由Hibernate的DDL:
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
)
我能怎麼做 ?
你打算是明星球員在球員被複制設定? – Jeremy
是的。相同的Player類可以在兩個集合中(作爲玩家和作爲明星)。 – Stephan
我發現這個鏈接可以幫助你:http://sieze.wordpress.com/2009/09/04/mapping-a-many-to-many-join-table-with-extra-column-using-jpa/ – Jeremy