我想模擬Player和Hero類之間的1:n關係。要說清楚:玩家應該有多個英雄,而英雄只屬於一個玩家。與JoinTable的OneToMany映射不會顯示在數據庫中
我的播放器類的相關摘錄(干將,setter和不相關性剝離)看起來是這樣的:
@Entity
@SequenceGenerator(name = "player_id", initialValue = 1, allocationSize = 1)
@Table(name = "players")
public class Player {
@OneToMany(cascade = CascadeType.ALL)
@JoinTable(name = "players_heroes",
joinColumns = { @JoinColumn(name = "player_id") },
inverseJoinColumns = { @JoinColumn(name = "hero_id") })
private Set<Hero> mHeroes;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "player_id", nullable = false)
private Long mId;
}
英雄類看起來很相似,目前沒有任何可能性,因爲即使是檢索所屬球員基本ASSOCATION尚不能工作:
@Entity
@SequenceGenerator(name = "hero_id", initialValue = 1, allocationSize = 1)
@Table(name = "heros")
public class Hero {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "hero_id", nullable = false)
private Long mId;
}
我目前使用hibernate.hbm2ddl.auto = create
以及它帶給我的是下面的輸出生成的數據庫模式:
Hibernate: create table heros (hero_id int8 not null, [...] primary key (hero_id))
Hibernate: create table players (player_id int8 not null, [...], primary key (player_id))
該聯想顯然缺失:(有人可以看到我做錯了什麼?
您是否需要其他連接表?連接表通常用於多對多關聯。 – Gary 2012-01-30 21:22:56
我需要以有效的方式列舉屬於玩家的所有英雄。不知何故,我認爲這會更快地使用連接表而不是連接列,但那是錯誤的,因爲我剛剛認識到。謝謝! – 2012-01-31 09:59:36