使用JoinTable的OneToMany映射不会显示在数据库中

时间:2012-01-29 10:00:23

标签: hibernate postgresql hibernate-mapping

我想模拟玩家和英雄级别之间的1:n关系。说清楚:玩家应该有多个英雄,而英雄只属于一个玩家。

我的播放器类的相关摘录(getter,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;
}

英雄级看起来非常相似,并且目前缺乏检索拥有玩家的任何可能性,因为即使基本关联还不起作用:

@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))

关联显然是缺失的:(任何人都可以看到我做错了吗?

1 个答案:

答案 0 :(得分:0)

好吧,只是在我给出的片段中看不到一个愚蠢的错误:一个失控的评论。