使用 JoinTable 的 OneToMany 映射不会显示在数据库中
我想在 Player 和 Hero 类之间建立 1:n 关系模型。明确一点:一个玩家应该拥有多个英雄,而一个英雄只能属于一个玩家。
我的玩家类的相关摘录(获取器、设置器和不相关的属性已被删除)如下所示:
@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))
关联明显缺失:( 有人能看到我做错了什么吗?
I would like to model a 1:n relationship between the Player and Hero class. To make it clear: A player should have multiple heroes whilst a hero only belongs to a single player.
The relevant excerpt (getters, setters and irrelevant properties stripped) of my player class looks like this:
@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;
}
The hero class looks quite similar and currently lacks any possibility to retrieve the owning player as even the basic assocation doesn't work yet:
@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;
}
I am currently generating the database schema by using hibernate.hbm2ddl.auto = create
and what it gives me is the following output:
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))
The associaton is clearly missing :( Can anybody see something I am doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,只是一个在我给出的片段中看不到的愚蠢错误:失控的评论。
Okay, just a dumb mistake not visible in the snippets I have given: A runaway comment.