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

发布于 2024-12-29 17:57:49 字数 1299 浏览 2 评论 0原文

我想在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泛滥成性 2025-01-05 17:57:49

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

Okay, just a dumb mistake not visible in the snippets I have given: A runaway comment.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文