【问题标题】:OneToMany mapping with JoinTable doesn't show up in the database使用 JoinTable 的 OneToMany 映射未显示在数据库中
【发布时间】:2012-02-21 13:34:10
【问题描述】:

我想模拟 Player 和 Hero 类之间的 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))

关联显然丢失了 :( 任何人都可以看到我做错了什么吗?

【问题讨论】:

  • 您还需要连接表吗?连接表通常用于多对多关联。
  • 我需要以一种有效的方式枚举属于玩家的所有英雄。不知何故,我认为使用连接表而不是连接列会更快,但我刚刚认识到这是错误的。谢谢!

标签: hibernate postgresql hibernate-mapping


【解决方案1】:

好的,只是我给出的 sn-ps 中看不到一个愚蠢的错误:一个失控的评论。

【讨论】:

  • 你应该考虑关闭这个问题,因为它对进一步的读者没有任何帮助。
猜你喜欢
  • 2018-10-14
  • 1970-01-01
  • 1970-01-01
  • 2019-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-23
  • 1970-01-01
相关资源
最近更新 更多